您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
================================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("我是学生了,姓名:{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) { //=============================集合初始值设定项 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("关羽", 150.00m); List<Person> p = new List<Person>() { p1, p2 };//集合初始值设定项 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) });//构造对象 //=============================插入元素 p.Insert(1, new Person("黄忠", 100.00m)); p.InsertRange(0, new Person[] { new Person("魏延", 80.00m), new Person("庞统", 100.00m) });//批量插入 p.InsertRange(0, new Person[] { new Person("魏延", 80.00m), new Person("庞统", 100.00m) });//批量插入 //=============================删除元素 //p.Remove(p1);//按对象删除 //p.RemoveAt(0);//按索引删除 p.RemoveRange(0, 1);//第一个参数开始索引位置;第二个参数删除的个数 //=============================查找元素 Console.WriteLine(p.Exists(r => r.Name == "张飞"));//找到返回True,否则返回false Console.WriteLine(p.IndexOf(p1)); //根据对象查找,【找到为1,没找到为-1】 Console.WriteLine(p.LastIndexOf(p1));//根据对象查找,从后往前查 Console.WriteLine(p.FindIndex(new FindPreson("庞统").Equals));//根据姓名查找,【返回索引】 Console.WriteLine(p.FindIndex(r => r.Name == "庞统"));//FindIndex lambda方式 Console.WriteLine(p.FindLastIndex(r => r.Name == "庞统"));//和FindIndex相似,从最后一个往前找 Console.WriteLine(p.Find(r => r.Name == "庞统").ToString());//根据姓名查找,【返回一个对象】 Console.WriteLine(p.FindAll(r => r.Name == "庞统")[0].ToString());//根据姓名查找,返回所有匹配对象 Console.WriteLine("=============="); //=============================排序 p.Sort();//没有参数的sort,person必须实现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()); //} //=============================强制转换 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; } } }
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。