c# list<T>操作实例

时间:2022-07-23
本文章向大家介绍c# list<T>操作实例,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
             List<string[]> str1 = new List<string[]> { };
             List<string[]> str2 = new List<string[]> { };
             string[] class1 = { "牛连山", "数学成绩", "58" };
             string[] class2 = { "孙少华", "数学成绩", "37" };
             string[] class3 = { "蒋大帅", "数学成绩", "45" };
             string[] class4 = new string[20];
             //str1.AddRange(str2);
             str1.Add(class1);
             str1.Add(class2);
             str1.Add(class3);
             Console.WriteLine("输出1:"+str1.Count());//结果是3
             Console.WriteLine("输出2:"+str1[0][0]);
             Console.WriteLine("输出3:"+(str1.OrderBy(s => s[2]).ToList())[2][2]);
       

            List<string> lists = new List<string> { };
            lists.Add("str");
            lists.Add("hello");
            string[] str = lists.ToArray();
            foreach(string aa in str)
            {
                Console.WriteLine(aa);
            }

            List<byte[]> byt1 = new List<byte[]> { };
            List<byte[]> byt2 = new List<byte[]> { };
            List<byte> byt3 = new List<byte> { };
            byte[] by1 = { 0x11, 0x03, 0x05 ,0x24};
            byte[] by2 = { 0xA9, 0x01, 0x56 };
            byte[] by3 = { 0x04, 0x02, 0x07 };
            byte[,] by4 = new byte[9,2];
            byte[] by5 = new byte[9];
            byte zijie;
            byt1.Add(by1);
            byt1.Add(by2);
            byt1.Add(by3);
            byt2.Add(by1);

            Console.WriteLine("输出4:" + byt1.Count());
            byt2 = byt1.OrderBy(s => s[1]).ToList();
            Console.WriteLine("输出字节:{0}-{1}-{2}", byt2[0][0],byt2[0][1],byt2[0][2]);
            zijie = byt2[0][0];
            Console.WriteLine("输出5:" + zijie);
            by4[0,1] = 0x11;
            Console.WriteLine(by4[0, 1].ToString());
            Console.WriteLine(byt2.ToArray()[0][0].ToString());
            foreach(byte[] item in byt2)
            {
              var  listdata = item.ToList();
                   foreach(var item1 in item)
                    Console.Write("--"+item1.ToString());
                Console.Write("--"+listdata.Count().ToString());
            }

                
            Console.ReadKey();

        }
    }
}