返回

C# List深入解析:揭秘集合泛型和深拷贝

后端

在程序设计中,集合是一项重要的数据结构,它能够存储和管理一组数据元素。C# List集合是开发人员常用的数据结构之一,它能够以有序的方式存储数据,并提供丰富的操作方法,以方便对数据的访问和管理。

C# List集合的定义和操作

C# List集合是基于System.Collections.Generic命名空间中的IList接口实现的,它可以存储一组具有相同数据类型的数据元素,并提供丰富的操作方法,包括添加、删除、修改和查找数据元素。

using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        // 创建一个List集合
        List<int> list = new List<int>();

        // 向List集合中添加元素
        list.Add(1);
        list.Add(2);
        list.Add(3);

        // 遍历List集合中的元素
        foreach (int item in list)
        {
            Console.WriteLine(item);
        }
    }
}

以上代码演示了如何使用C# List集合存储和遍历数据。首先,我们使用System.Collections.Generic命名空间中的List类创建了一个整型List集合。然后,我们使用Add方法向集合中添加了三个元素。最后,我们使用foreach循环遍历了集合中的元素并将其输出到控制台。

C# List集合的泛型特性

C# List集合支持泛型,这意味着它可以存储任何类型的数据元素。泛型可以提高代码的可重用性,并使代码更加简洁和安全。

class Program
{
    static void Main(string[] args)
    {
        // 创建一个List集合,存储字符串类型的数据
        List<string> list = new List<string>();

        // 向List集合中添加元素
        list.Add("Hello");
        list.Add("World");
        list.Add("!");

        // 遍历List集合中的元素
        foreach (string item in list)
        {
            Console.WriteLine(item);
        }
    }
}

以上代码演示了如何使用C# List集合存储字符串类型的数据。我们使用System.Collections.Generic命名空间中的List类创建了一个字符串List集合。然后,我们使用Add方法向集合中添加了三个元素。最后,我们使用foreach循环遍历了集合中的元素并将其输出到控制台。

C# List集合的深拷贝

深拷贝是一种数据复制技术,它会复制数据的全部内容,包括数据本身和数据引用的对象。深拷贝与浅拷贝不同,浅拷贝只复制数据的引用,而不复制数据本身。

class Program
{
    static void Main(string[] args)
    {
        // 创建一个List集合,存储对象类型的数据
        List<Person> list = new List<Person>();

        // 向List集合中添加元素
        list.Add(new Person("John", 20));
        list.Add(new Person("Mary", 25));

        // 创建一个新List集合,使用深拷贝复制旧List集合中的数据
        List<Person> newList = new List<Person>(list);

        // 修改旧List集合中的元素
        list[0].Name = "Tom";

        // 输出两个List集合中的元素
        Console.WriteLine("Old List:");
        foreach (Person item in list)
        {
            Console.WriteLine(item);
        }

        Console.WriteLine("New List:");
        foreach (Person item in newList)
        {
            Console.WriteLine(item);
        }
    }

    class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }

        public Person(string name, int age)
        {
            Name = name;
            Age = age;
        }

        public override string ToString()
        {
            return 
class Program
{
    static void Main(string[] args)
    {
        // 创建一个List集合,存储对象类型的数据
        List<Person> list = new List<Person>();

        // 向List集合中添加元素
        list.Add(new Person("John", 20));
        list.Add(new Person("Mary", 25));

        // 创建一个新List集合,使用深拷贝复制旧List集合中的数据
        List<Person> newList = new List<Person>(list);

        // 修改旧List集合中的元素
        list[0].Name = "Tom";

        // 输出两个List集合中的元素
        Console.WriteLine("Old List:");
        foreach (Person item in list)
        {
            Console.WriteLine(item);
        }

        Console.WriteLine("New List:");
        foreach (Person item in newList)
        {
            Console.WriteLine(item);
        }
    }

    class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }

        public Person(string name, int age)
        {
            Name = name;
            Age = age;
        }

        public override string ToString()
        {
            return $"{Name} ({Age})";
        }
    }
}
quot;{Name} ({Age})"
; } } }

以上代码演示了如何使用C# List集合的深拷贝功能。我们首先创建了一个List集合,存储Person对象类型的数据。然后,我们向集合中添加了两个Person对象。接着,我们使用List类的构造函数创建了一个新List集合,并使用深拷贝复制旧List集合中的数据。然后,我们修改了旧List集合中的第一个Person对象的名字。最后,我们输出两个List集合中的元素,我们可以看到,新List集合中的Person对象的名字没有改变,这说明深拷贝成功地复制了数据本身,而不是数据引用。

结论

C# List集合是一种功能强大的数据结构,它支持泛型和深拷贝,能够以有序的方式存储和操作数据。List集合在C#编程中非常有用,它可以用于各种各样的场景。