Ilist 的深入学习和ArrayList中Cast<>的使用

在项目中经常看到判断null的判断,对这个有无必要执怀疑,有空就测试了一下,在列表创建之后就已不是空,所以完全无必要

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> list = new List<int>();
            IList<int> list1 = new List<int> { 1, 2 };
            IList<int> list2 = new List<int> { 3, 5 };
            if (list == null)
            {
                Console.WriteLine("list is null");
                
            }
            else
            {
                Console.WriteLine("list is not null");

            }
            for(int i = 0; i < list1.Count; i++)
            {
                list2.Add(list1[i]);
            }

            for(int i = 0; i < list2.Count; i++)
            {
                Console.WriteLine(list2[i]);
            }
            Console.Read();
        }
    }
}
     结果:
list is not null
3
5
1
2

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Collections.ArrayList fruits = new System.Collections.ArrayList();
            fruits.Add("mango");
            fruits.Add("apple");
            fruits.Add(1);   // 这个转换时会报错, int32无法转换为string类型

            IEnumerable<string> query = fruits.Cast<String>().OrderBy(fruit => fruit).Select(fruit => fruit);

            foreach (string fruit in query)
            {
                Console.WriteLine(fruit);
            }

            Console.Read();
        }
    }
}




`List<>``ArrayList`存在多方面区别: ### 泛型与非泛型 `List<>`是泛型集合,在声明时需要指定集合内数据的对象类型,例如`List<int>`只能存储整数类型的数据,`List<string>`只能存储字符串类型的数据。而`ArrayList`是非泛型集合,可以存储任意类型的对象,例如: ```csharp // List<> 示例 List<int> intList = new List<int>(); intList.Add(1); // 以下代码会编译错误,因为不能添加字符串到 List<int> 中 // intList.Add("hello"); // ArrayList 示例 System.Collections.ArrayList arrayList = new System.Collections.ArrayList(); arrayList.Add(1); arrayList.Add("hello"); ``` 这体现了`List<>`在编译时就进行类型检查,而`ArrayList`在运行时才进行类型检查。 ### 类型安全 由于`List<>`是泛型集合,编译器会在编译时检查添加到集合中的元素类型是否符合声明的类型,从而避免了运行时的类型转换错误,保证了类型安全。而`ArrayList`可以存储任意类型的对象,在从`ArrayList`中获取元素时,需要进行显式的类型转换,如果类型转换不正确,会在运行时抛出`InvalidCastException`异常,例如: ```csharp System.Collections.ArrayList arrayList = new System.Collections.ArrayList(); arrayList.Add("hello"); // 以下代码需要显式类型转换 string str = (string)arrayList[0]; List<string> stringList = new List<string>(); stringList.Add("hello"); // 不需要显式类型转换 string str2 = stringList[0]; ``` ### 性能差异 `ArrayList`存在装箱拆箱操作。当向`ArrayList`中添加值类型(如`int`、`double`等)时,会将值类型装箱为`object`类型;从`ArrayList`中获取值类型时,又需要将`object`类型拆箱为值类型。装箱拆箱操作会带来一定的性能开销。而`List<>`在存储值类型时,不需要进行装箱拆箱操作,因此在处理值类型时,`List<>`的性能要优于`ArrayList`。 ### 继承接口 `List<>``ArrayList`都继承了`IList`接口,但`List<>`是`ArrayList`类的泛型等效类,`List<>`还实现了`IEnumerable<T>`等泛型接口,提供了更丰富的泛型操作方法[^1]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值