C \ # List 방법의 사용

4025 단어 C#
  class Collection
    {
        /// 
        ///     :  
        /// 
        List list = new List();

       
        public static void Test()
        {
            Collection c = new Collection();
            c.listTest();
            Console.ReadKey();
        }

        private void InitList()
        {
            Student st1 = new Student("  ", 17, 1000);
            Student st2 = new Student("  ", 20, 1001);
            Student st3 = new Student("  ", 18, 1000);
            Student st4 = new Student("  ", 19, 1001);
            Student st5 = new Student("  ", 18, 1003);
            Student st6 = new Student("  ", 21, 1002);
            list.Add(st1);
            list.Add(st2);
            list.Add(st3);
            list.Add(st4);
            list.Add(st5);
            list.Add(st6);
        }

        public void listTest()
        {
            this.InitList();

            foreach (var item in list)
            {
                Console.WriteLine(item);
            }

           
            Console.WriteLine("
***** Id *****"); var query = list.GroupBy(pet => pet.Id); // IEnumerable> query = list.GroupBy((st) => { return st.Id; }); // IEnumerable> query = list.GroupBy(ListGroud); foreach (var item in query) { List stt = item.ToList(); Console.WriteLine("key::" + item.Key); foreach (var item2 in stt) { Console.WriteLine(item2); } Console.WriteLine(""); } Console.WriteLine("
:" + list.BinarySearch(list[2])); Console.WriteLine("
A, A key ( ):"); var dictionary2 = list.ToDictionary(st => st.Name); foreach (var item in dictionary2) { Console.WriteLine("key:" + item.Key + " ->value:" + item.Value); } Console.WriteLine("
:"); var tempList = list.Where(a => a.Name.Contains(" ") || a.Name.Contains(" ")).ToList(); // List tempList = list.FindAll(st => st.Name.Contains(" ") || st.Name.Contains(" ")); foreach (var item in tempList) { Console.WriteLine(item); } Console.WriteLine("
:" + list.Sum(st => st.Age)); ; list.Take(3); // 3 list.Skip(4); // ( 4 ) var kk = list.GetRange(1, 3); // 1 , , 。 bool b = list.Exists(st => st.Name == " "); // var templist = list.OrderBy(st => st.Id); // list.Sort(); // IComparable foreach (var item in templist) { Console.WriteLine(item); } int maxAge= list.Max(st => st.Age); Student ss= list.Max(); } private int ListGroud(Student st) { return st.Id; } } public class Student:IComparable { public int Age; public int Id; public string Name; public Student(string name, int age, int Id) { this.Name = name; this.Age = age; this.Id = Id; } public override string ToString() { return string.Format(" :{0}、 :{1}、 :{2}", this.Id, this.Name, this.Age); } public int CompareTo(object obj) { return this.Id > ((Student)obj).Id ? 1 : -1; } }

좋은 웹페이지 즐겨찾기