C#教程
C#控制语句
C#函数
C#数组
C#面向对象
C#命名空间和异常
C#文件IO
C#集合
C#多线程
C#其它

C# List<T>

C# List<T>

C# List 类用于存储和获取元素。它可以有重复的元素。它位于 System.Collections.Generic 命名空间中。

C# List 示例

让我们看一个存储元素的泛型 List 类的示例使用 Add() 方法并使用 for-each 循环迭代列表。
using System;
using System.Collections.Generic;
public class ListExample
{
    public static void Main(string[] args)
    {
        // Create a list of strings
        var names = new List<string>();
        names.Add("Sonoo Jaiswal");
        names.Add("Ankit");
        names.Add("Peter");
        names.Add("Irfan");
        // Iterate list element using foreach loop
        foreach (var name in names)
        {
            Console.WriteLine(name);
        }
    }
}
输出:
 Sonoo Jaiswal
Ankit
Peter
Irfan

C# List 使用集合初始值设定项的示例

using System;
using System.Collections.Generic;
public class ListExample
{
    public static void Main(string[] args)
    {
        // Create a list of strings using collection initializer
        var names = new List<string>() {"Sonoo", "Vimal", "Ratan", "Love" };
       
        // Iterate through the list.
        foreach (var name in names)
        {
            Console.WriteLine(name);
        }
    }
}
输出:
 Sonoo
Vimal
Ratan
Love
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4