List

List 是使用数组实现的一种数据结构,其大小可以根据需要动态增加。List允许重复的数据存在,如果要调用其Sort方法排序,必须保证类型T实现了IComparable接口。

List 在命名空间System.Collections.Generic下,其主要用于存储类型明确(强类型)的一组对象,常用的方法如下:

List < string > colors = new List < string > ();

//add items in a List collection
colors.Add("Red");
colors.Add("Blue");
colors.Add("Green");

//insert an item in the list
colors.Insert(1, "violet");

//retrieve items using foreach loop
foreach (string color in colors)
{
    MessageBox.Show(color);
}

//remove an item from list
colors.Remove("violet");

//copy array to list
string[] strArr = new string[3];
strArr[0] = "Red";
strArr[1] = "Blue";
strArr[2] = "Green";
List < string > arrlist = new List < string > (strArr);

foreach (string str in strArr)
{
    MessageBox.Show(str);
}

//call clear method
arrlist.Clear ();

List是实现了IList接口的一个类,一般使用时,推荐按照如下方法使用:

IList list = new List();

这样定义,可以达到松耦合的目的,如果以后不使用List结构了,改使用实现了IList的其它结构时,更方便更改。

来看以下示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ConsoleApplication1
{
    internal class Program
    {
        public static void Main()
        {
            var employees = new List<Employee>();
            employees.Add(new Employee { Name = "Owen", Id = 1000 });
            employees.Add(new Employee { Name = "Vincent", Id = 1001 });
            employees.Add(new Employee { Name = "Lynn", Id = 1002 });
            employees.Add(new Employee { Name = "Ricy", Id = 1003 });

            // output all the elements
            foreach (var employee in employees)
                Console.WriteLine(employee);

            if (employees.Contains(new Employee { Name = "Owen", Id = 1000 }))
                Console.WriteLine("Owen contained");
            else
                Console.WriteLine("Owen not exists");

            Console.ReadLine();
        }
    }
}

使用Contain方法检验是否存在名为Owen的员工,运行结果如下:

运行结果

之所以显示员工Owen不存在,是因为使用Contain时,传入的参数是一个新建的Employee对象,这与List中原本有的对象不相同,尽管值是相同的。如果List中存储的是string类型的对象就不会有这个问题。

using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    class Program
    {
        public static void Main()
        {
            var employees = new List<string>();
            employees.Add("Owen");
            employees.Add("Vincent");
            employees.Add("Lynn");
            employees.Add("Ricy");

            string owen = "Owen";
            if (employees.Contains(owen))
                Console.WriteLine("Owen exists");

            Console.ReadLine();
        }
    }
}

运行结果:
运行结果

之所以出现这种差别,是因为string类型实现了IEquatable接口,自定义可比性。将Employee定义如下即可:


using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    class Employee : IEquatable<Employee>
    {
        private string name;
        private int id;

        public string Name
        {
            get { return this.name; }
            set { this.name = value; }
        }

        public int Id
        {
            get { return this.id; }
            set { this.id = value; }
        }

        public override string ToString()
        {
            return this.Name + ", " + this.id;
        }

        public bool Equals(Employee other)
        {
            return this.name.Equals(other.name);
        }
    }
}

运行结果:
运行结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值