C#List<T>类

C#List<T>类 首页 / C#入门教程 / C#List<T>类

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

C# List<T> example

让无涯教程看一个泛型List<T>类的示例,该类使用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();
        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);
        }
    }
}

输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/csharp/c-sharp-list.html

来源:LearnFk无涯教程网

Sonoo Jaiswal
Ankit
Peter
Irfan

C# List<T> 使用集合初始化器的示例

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() {"Sonoo", "Vimal", "Ratan", "Love" };
       
        //Iterate through the list.
        foreach (var name in names)
        {
            Console.WriteLine(name);
        }
    }
}

输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/csharp/c-sharp-list.html

来源:LearnFk无涯教程网

Sonoo
Vimal
Ratan
Love

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

教程推荐

AI大模型之美 -〔徐文浩〕

云原生架构与GitOps实战 -〔王炜〕

业务开发算法50讲 -〔黄清昊〕

郭东白的架构课 -〔郭东白〕

陈天 · Rust 编程第一课 -〔陈天〕

MongoDB高手课 -〔唐建法(TJ)〕

零基础学Java -〔臧萌〕

趣谈网络协议 -〔刘超〕

从0开始学架构 -〔李运华〕

好记忆不如烂笔头。留下您的足迹吧 :)