Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

c# list

作者:ˉ吻干迩的'泪   发布日期:2025-02-07   浏览:117

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        // 创建一个 List<string> 实例
        List<string> stringList = new List<string>();

        // 添加字符串到列表中
        stringList.Add("Hello");
        stringList.Add("World");
        stringList.Add("!");

        // 遍历并打印列表中的每个元素
        foreach (string item in stringList)
        {
            Console.WriteLine(item);
        }

        // 检查列表是否包含某个特定的字符串
        if (stringList.Contains("World"))
        {
            Console.WriteLine("列表中包含 'World'");
        }

        // 删除列表中的某个元素
        stringList.Remove("!");

        // 输出删除后的列表内容
        Console.WriteLine("删除后的列表内容:");
        foreach (string item in stringList)
        {
            Console.WriteLine(item);
        }
    }
}

解释说明:

  1. 创建 List<string>:

    • 使用 new List<string>() 创建一个可以存储字符串的列表。
  2. 添加元素:

    • 使用 Add() 方法向列表中添加字符串。
  3. 遍历和打印:

    • 使用 foreach 循环遍历列表,并使用 Console.WriteLine() 打印每个元素。
  4. 检查元素是否存在:

    • 使用 Contains() 方法检查列表中是否包含某个特定的字符串。
  5. 删除元素:

    • 使用 Remove() 方法从列表中删除指定的字符串。
  6. 输出删除后的列表内容:

    • 再次遍历并打印列表,以显示删除操作后的结果。

上一篇:c#double保留两位小数

下一篇:c#序列化

大家都在看

c# 二进制

c# tcp client

c# type.gettype

c# sqlconnection

.net和c#

c#游戏开发

c#网络编程

c# rectangle

c# if else

c# rtsp

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站