欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 文化 > 4.3 数组和集合的初始及赋值

4.3 数组和集合的初始及赋值

2025/3/14 21:05:53 来源:https://blog.csdn.net/UruseiBest/article/details/145984539  浏览:    关键词:4.3 数组和集合的初始及赋值

版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的

版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的

C#中数组和集合初始化以及赋值的常见代码如下:

            //========= 数组 ===========

            //1

            int[] a1 = { 1, 2, 3, 4, 5 };

            //2

            int[] a2 = new int[5] { 1, 2, 3, 4, 5 };

            //3 二维数组

            int[,] a3 = { { 1, 2 }, { 3, 4 }, { 5, 6 } };

            //4 二维数组

            int[,] a4 = new int[3, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 } };

            //5 二维数组

            var a5 = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 } };

            //6 数组的数组,每个子数组可以有不同的长度

            var a6 = new int[][] { new int[] { 1, 2 }, new int[] { 3, 4 }, new int[] { 5, 6 } };

            for (int i = 0 ;i< a6.Length ;i++)

            {

                for(int j = 0 ;j< a6[i].Length ;j++)

                    Console.Write(a6[i][j] + " ");

                Console.WriteLine("");

            }

            //========= ArrayList ===========

            //1

            ArrayList alst1 = new ArrayList();

            alst1.Add("12");

            alst1.Add(34);

            alst1.Add("56");

            //2

            string[] alstsample = { "12", "34", "56" };

            ArrayList alst2 = new ArrayList(alstsample);

            //3

            object[] alstsample1 = { 12, "ab", 34 };

            ArrayList alst3 = new ArrayList(alstsample1);

            //4

            ArrayList alst4 = new ArrayList();

            alst4.AddRange(new string[] { "12", "34", "56" });

            //5

            ArrayList alst5 = new ArrayList(new string[] { "12", "34", "56" });

            //6

            ArrayList alst6 = new ArrayList(new object[] { "ab", 12, "34" });

            //7

            ArrayList alst7 =new ArrayList(){12, "ab", 34};

            //=========== List =================

            //1

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

            lst1.Add("12");

            lst1.Add("34");

            lst1.Add("56");

            //2

            string[] lstsample = { "12", "34", "56" };

            List<string> lst2 = new List<string>(lstsample);

            //3

            List<string> lst3 = new List<string>(new string[] { "12", "34", "56" });

            //4

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

            lst4.AddRange(new string[] { "12", "34", "56" });

            //5

        List<string> lst5 = new List<string> { "12", "34", "56" }; 

学习更多vb.net知识,请参看vb.net 教程 目录

学习更多C#知识,请参看C#教程 目录

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词