信息查询系统总结--C#list的使用

来源:互联网 发布:弹性光网络 编辑:程序博客网 时间:2024/05/29 02:25

List类是 ArrayList 类的泛型等效类。该类使用大小可按需动态增加的数组实现 IList 泛型接口。

泛型的好处: 它为使用c#语言编写面向对象程序增加了极大的效力和灵活性。不会强行对值类型进行装箱和拆箱,或对引用类型进行,向下强制类型转换,所以性能得到提高。

下面用实例总结list的用法:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ListGrammer{    public class Student    {        public string SNO { get; set; }        public string Name { get; set; }        public string Gender { get; set; }        public string Mobile { get; set; }        public string Email { get; set; }        public string HomeAddress { get; set; }    }    class Program    {        static void Main(string[] args)        {            //list的定义            List<Student> objstudent=new List<Student>();            List<int[]> objint = new List<int[]>();            List<string> objtext = new List<string>();            //添加元素            objstudent.Add(new Student  //类的初始化方法            {                SNO = "151530018",                Name = "王大帅",                Gender = "男",                Mobile = "110",                Email = "110@163.com",                HomeAddress = "everybody's 隔壁"            }            );            int[] array = { 1, 2, 3, 4, 5 };            objint.Add(array);            objtext.Add("你呀");            objtext.Add("hello world");            objtext.Add("hello zhang");            Console.WriteLine(objstudent[0].Name+ objint[0][1].ToString()+ objtext[0]);            //删除元素            objtext.Remove("hello world");//既然是string类型的list,元素就是string            objtext.RemoveAt(0);//删除索引为0的字符串            objtext.RemoveRange(0, 1);//删除索引为0起的一个元素            Console.WriteLine("此时objtext的元素个数:{0}",objtext.Count);            //将指定集合的元素添加到list的末尾(快速向list添加元素)            string[] arraystring = { "mike", "joke", "mary", "jone", "alice", "role" };            objtext.AddRange(arraystring);            //遍历list            foreach (string item in objtext)            {                Console.Write(item + "   ");            }            Console.Write("\n");            //排序            Console.WriteLine("默认排序后:");            objtext.Sort();            foreach (string item in objtext)            {                Console.Write(item + "   ");            }            Console.Write("\n");            //反转            Console.WriteLine("把索引为1开始的3个元素反转后:");            objtext.Reverse(1, 3);            foreach (string item in objtext)            {                Console.Write(item + "   ");            }            Console.Write("\n");            //objtext.Contains(item);            //objtext.Clear();            //            Console.ReadKey();        }    }}

运行结果:
这里写图片描述

原创粉丝点击