《C#数据结构与算法》--2020 最新精讲版:2-6 装箱和拆箱

本文主要通过观看B站上的《C#数据结构与算法》教程,探讨了C#中装箱和拆箱的概念,并通过四个测试比较了泛型List与非泛型ArrayList在处理值类型和引用类型时的性能差异。测试结果显示,泛型List在处理值类型时性能更优,而泛型List和ArrayList在处理引用类型时性能相近。文章还解释了为何C#同时保留ArrayList和List的原因。
摘要由CSDN通过智能技术生成

目录

一.目的

1.想:将B站视频《C#数据结构与算法》--2020 最新精讲版:提高学习效率,所以编写此系列博客

1.因为这个系列教程评价目前是最好的,所以想看视频、写代码、写博客

二.参考

1.C#数据结构与算法》--2020 最新精讲版

三.操作:1:成功

1.版本

1.课程目的:装箱和拆箱

1.1 解决问题的方法:

1.装箱和拆箱:概念

1.检查哪些是引用类型、哪些是值类型

1.1 值类型:

1.1 引用类型:

 1.引用库:计时的库

1. 测试1:泛型List+int值类型

1.测试2:非泛型ArrayList+int值类型

1.测试3:泛型List+引用类型对象string

1.测试4:非泛型ArrayList+ 引用类型string

1.运行结果:泛型+int值类型花费时间最短

1.泛型和非泛型:泛型数组List有俩个优势:

1.为什么有了List还需要ArrayList?因为C#现有ArrayList,后又List,如果去除ArrayList,会导致老工程报错,所以一直保留了下来


一.目的

1.想:将B站视频《C#数据结构与算法》--2020 最新精讲版:提高学习效率,所以编写此系列博客

1.因为这个系列教程评价目前是最好的,所以想看视频、写代码、写博客

二.参考

1.C#数据结构与算法》--2020 最新精讲版

https://www.bilibili.com/video/BV1gE41157pC

  1. 学习的视频

三.操作:1:成功

1.版本

  1. windows10 64
  2. VS2019

1.课程目的:装箱和拆箱

  1. 解决之前静态数组的局限性:之前课程编写的数组是int型,无法存储其他数据类型的数组,

1.1 解决问题的方法:

方法一:将int型改为object:比如ArrayList

  1. 将int型改为object,然后将object强制转换为我们需要的数据类型,其中ArrayList就是这种方法
  2. 但是这样会造成 装箱、拆箱,性能上面大大折扣,

方法二:泛型:比如:List

  1. 相比较ArrayList会有很好地优势
  2. 后面课程会对ArrayList、List性能测试。

1.装箱和拆箱:概念

 

1.检查哪些是引用类型、哪些是值类型

1.1 值类型:

  1. int按下F12按键后,查看,发现是struct Int32,所以是值类型

1.1 引用类型:

  1. class修饰的为引用类型

 1.引用库:计时的库

1. 测试1:泛型List+int值类型

            int n = 10000000;
            Stopwatch t1 = new Stopwatch();
            Stopwatch t2 = new Stopwatch();
            Stopwatch t3 = new Stopwatch();
            Stopwatch t4 = new Stopwatch();

            //测试1:泛型List+int值类型
            t1.Start();
            List<int> l = new List<int>();
            for (int i = 0; i <n; i++)
            {
                l.Add(i);//int:值类型+List:不发生装箱
                int x = l[i];//int:值类型+List:不发生拆箱
            }
            t1.Stop();
            Console.WriteLine("测试1:泛型List+int值类型 花费时间:" + t1.ElapsedMilliseconds + "ms");

1.测试2:非泛型ArrayList+int值类型

           //测试2:非泛型ArrayList+int值类型
            t2.Start();
            ArrayList a = new ArrayList();
            for (int i = 0; i <n; i++)
            {
                a.Add(i);//发生装箱:非泛型ArrayList+int值类型
                int x = (int)a[i];//发生拆箱:非泛型ArrayList+int值类型
            }
            t2.Stop();
            Console.WriteLine("测试2:非泛型ArrayList+int值类型 花费时间:" + t2.ElapsedMilliseconds + "ms");

1.测试3:泛型List+引用类型对象string

            //测试3:泛型List+引用类型对象string
            t3.Start();
            List<string> l2 = new List<string>();
            for (int i = 0; i <n; i++)
            {
                l2.Add("X");//不发生装箱
                string x = l2[i];//不发生拆箱
            }
            t3.Stop();
            Console.WriteLine("测试3:泛型List+引用类型对象string:花费时间:" + t3.ElapsedMilliseconds+"ms");

1.测试4:非泛型ArrayList+ 引用类型string

1.运行结果:泛型+int值类型花费时间最短

           int n = 10000000;
            Stopwatch t1 = new Stopwatch();
            Stopwatch t2 = new Stopwatch();
            Stopwatch t3 = new Stopwatch();
            Stopwatch t4 = new Stopwatch();

            //测试1:泛型List+int值类型
            t1.Start();
            List<int> l = new List<int>();
            for (int i = 0; i <n; i++)
            {
                l.Add(i);//int:值类型+List:不发生装箱
                int x = l[i];//int:值类型+List:不发生拆箱
            }
            t1.Stop();
            Console.WriteLine("测试1:泛型List+int值类型 花费时间:" + t1.ElapsedMilliseconds + "ms");

            //测试2:非泛型ArrayList+int值类型
            t2.Start();
            ArrayList a = new ArrayList();
            for (int i = 0; i <n; i++)
            {
                a.Add(i);//发生装箱:非泛型ArrayList+int值类型
                int x = (int)a[i];//发生拆箱:非泛型ArrayList+int值类型
            }
            t2.Stop();
            Console.WriteLine("测试2:非泛型ArrayList+int值类型 花费时间:" + t2.ElapsedMilliseconds + "ms");

            //测试3:泛型List+引用类型对象string
            t3.Start();
            List<string> l2 = new List<string>();
            for (int i = 0; i <n; i++)
            {
                l2.Add("X");//不发生装箱
                string x = l2[i];//不发生拆箱
            }
            t3.Stop();
            Console.WriteLine("测试3:泛型List+引用类型对象string:花费时间:" + t3.ElapsedMilliseconds+"ms");

            //测试4:非泛型ArrayList+ 引用类型string
            t4.Start();
            ArrayList a2 = new ArrayList();
            for (int i = 0; i <n; i++)
            {
                a2.Add("X");//不发生装箱
                string s = (string)a2[i];//不发生拆箱
            }
            t4.Stop();
            Console.WriteLine("测试4:非泛型ArrayList+ 引用类型string 花费时间:" + t4.ElapsedMilliseconds + "ms");

            Console.Read();

1.泛型和非泛型:泛型数组List有俩个优势:

 

  1. List不需要进行装箱、拆箱,就不会出现强制转换时候出现问题,所以更好;

1.为什么有了List还需要ArrayList?因为C#先有ArrayList,后又List,如果去除ArrayList,会导致老工程报错,所以一直保留了下来

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值