您好,登錄后才能下訂單哦!
================================Person.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace ConsoleApplication2 { public class Person : IComparable<Person>, IFormattable { public decimal Money { get; private set; } public string Name { get; private set; } public Person(string name,decimal money ) { this.Name = name; this.Money = money; } public string ToString(string format, IFormatProvider formatProvider) { if (format == null) return ToString(); switch (format) { case "N": return string.Format("姓名:{0}", Name); case "M": return string.Format("收入:{0}", Money); default: return ToString(); } } public override string ToString() { return string.Format("姓名:{0},收入:{1}", Name, Money); } public int CompareTo(Person other) { int resouce = other.Money.CompareTo(this.Money);//降序 if (resouce == 0) { resouce = other.Name.CompareTo(this.Name); } return resouce; } } }
================================Student.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication2 { [Serializable]//指示一個類可以序列化,不可以繼承 public class Student { public string Name { get; set; } public Student(string name) { this.Name = name; } public override string ToString() { return string.Format("我是學(xué)生了,姓名:{0}", this.Name); } } }
================================主程序
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { //=============================集合初始值設(shè)定項 List<int> i = new List<int>() { 1, 2 }; List<string> s = new List<string>() { "a", "b" }; //=============================添加元素 Person p1 = new Person("張飛", 100.00m); Person p2 = new Person("關(guān)羽", 150.00m); List<Person> p = new List<Person>() { p1, p2 };//集合初始值設(shè)定項 p.Add(new Person("諸葛亮", 500.00m)); p.AddRange(new Person[] { new Person("趙云", 50.00m), new Person("馬超", 100.00m) });//添加集合 List<Person> per = new List<Person>(new Person[] { new Person("趙云", 50.00m), new Person("馬超", 100.00m) });//構(gòu)造對象 //=============================插入元素 p.Insert(1, new Person("黃忠", 100.00m)); p.InsertRange(0, new Person[] { new Person("魏延", 80.00m), new Person("龐統(tǒng)", 100.00m) });//批量插入 p.InsertRange(0, new Person[] { new Person("魏延", 80.00m), new Person("龐統(tǒng)", 100.00m) });//批量插入 //=============================刪除元素 //p.Remove(p1);//按對象刪除 //p.RemoveAt(0);//按索引刪除 p.RemoveRange(0, 1);//第一個參數(shù)開始索引位置;第二個參數(shù)刪除的個數(shù) //=============================查找元素 Console.WriteLine(p.Exists(r => r.Name == "張飛"));//找到返回True,否則返回false Console.WriteLine(p.IndexOf(p1)); //根據(jù)對象查找,【找到為1,沒找到為-1】 Console.WriteLine(p.LastIndexOf(p1));//根據(jù)對象查找,從后往前查 Console.WriteLine(p.FindIndex(new FindPreson("龐統(tǒng)").Equals));//根據(jù)姓名查找,【返回索引】 Console.WriteLine(p.FindIndex(r => r.Name == "龐統(tǒng)"));//FindIndex lambda方式 Console.WriteLine(p.FindLastIndex(r => r.Name == "龐統(tǒng)"));//和FindIndex相似,從最后一個往前找 Console.WriteLine(p.Find(r => r.Name == "龐統(tǒng)").ToString());//根據(jù)姓名查找,【返回一個對象】 Console.WriteLine(p.FindAll(r => r.Name == "龐統(tǒng)")[0].ToString());//根據(jù)姓名查找,返回所有匹配對象 Console.WriteLine("=============="); //=============================排序 p.Sort();//沒有參數(shù)的sort,person必須實現(xiàn)IComparable<T>接口,倒序 p.Sort(new SortPerson(PersonType.Money));//倒序 p.Sort((x, y) => x.Money.CompareTo(y.Money));//正序 //=============================訪問元素(for,foreach) //p.ForEach(Console.WriteLine);//委托Console.WriteLine顯式每一項的值 p.ForEach(r => Console.WriteLine(r));//使用lambda表達式 //foreach (var item in p) //{ // Console.WriteLine(item.ToString()); //} //=============================強制轉(zhuǎn)換 List<Student> stu = p.ConvertAll(r => new Student(r.Name)); stu.ForEach(Console.WriteLine); Console.ReadKey(); } private static bool HandleAction(Person p) { Console.WriteLine(p.Name); return true; } } //用于比較 class FindPreson : IEquatable<Person> { private string Name; public FindPreson(string name) { this.Name = name; } public override int GetHashCode() { return base.GetHashCode(); } public override bool Equals(object obj) { if (obj == null) throw new ArgumentException("對象不能為空"); return Equals(obj as Person); } public bool Equals(Person other) { if (other == null) throw new ArgumentException("對象不能為空"); return this.Name == other.Name; } } //用于排序 enum PersonType { Name, Money } class SortPerson:IComparer<Person> { PersonType pt; public SortPerson(PersonType p) { pt = p; } public int Compare(Person x, Person y) { if (x == null || y == null) throw new ArgumentException("對象不能為null"); int source = 0; switch (pt) { case PersonType.Money: source = y.Money.CompareTo(x.Money);//降序 break; case PersonType.Name: source = y.Name.CompareTo(x.Name);//降序 break; default: source = y.Money.CompareTo(x.Money);//降序 break; } return source; } } }
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。