C# .Net 实例积累

 1. 类、对象、接口、继承 实例:

 using System; using System.Collections.Generic; using System.Text; namespace _9ex01 { public abstract class MyBase { } internal class MyClass : MyBase { } public interface IMyBaseInterface { } internal interface IMyBaseInterface2 { } internal interface MyInterface : IMyBaseInterface, IMyBaseInterface2 { } internal sealed class MyComplexClass : MyClass, MyInterface { } class Program { static void Main(string[] args) { MyComplexClass myObj = new MyComplexClass(); Console.WriteLine(myObj.ToString()); Console.ReadKey(); } } }

 

 2. IComparable, Icomparer 接口

using System; using System.Collections.Generic; using System.Collections; using System.Text; namespace IComparable21 { class Student : IComparable { private string _name; private int _age; public Student(string name, int age) { _name = name; _age = age; } public string Name { get { return _name; } set { _name = value; } } public int Age { get { return _age; } set { _age = value; } } int IComparable.CompareTo(object obj) { if (!(obj is Student)) { throw new ArgumentException("The arguments must be in type of Student."); } return _name.CompareTo(((Student)obj)._name); } public int CompareTo(Student obj) { return _age.CompareTo(obj._age); } private static AgeComparer _ageCom = null; public static IComparer AgeCom { get { if (_ageCom == null) { _ageCom = new AgeComparer(); } return _ageCom; } } private class AgeComparer : IComparer { int IComparer.Compare(object left, object right) { if (!(left is Student) || !(right is Student)) { throw new ArgumentException("The arguments must be in type of Student."); } return ((Student)left)._age.CompareTo(((Student)left)._age); } } } class Program { static void Main(string[] args) { Student[] arr = new Student[5]; arr[0] = new Student("张三", 5); arr[1] = new Student("李四", 3); arr[2] = new Student("王五", 1); arr[3] = new Student("赵六", 4); arr[4] = new Student("钱七", 6); Console.WriteLine("Sort as _name:"); Array.Sort(arr); foreach (Student s in arr) { Console.WriteLine(s.Name + " 年龄:" + s.Age); } Console.WriteLine("Sort as _age:"); Array.Sort(arr,Student.AgeCom); foreach (Student s in arr) { Console.WriteLine(s.Name + " 年龄:" + s.Age); } Console.ReadKey(); } } }

 3. 事件

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace event2 { class PubEventArgs : EventArgs { private readonly string _magazineName; private readonly DateTime _pubDate; public PubEventArgs(string magazineName, DateTime pubDate) { _magazineName = magazineName; _pubDate = pubDate; } public string magazineName { get { return _magazineName; } } public DateTime pubDate { get { return _pubDate; } } } class Publisher { public delegate void PubComputerEventHandler(object sender,PubEventArgs e); public delegate void PubLifeEventHandler(object sender, PubEventArgs e); public event PubComputerEventHandler PubComputer; public event PubLifeEventHandler PubLife; protected virtual void OnPubComputer(PubEventArgs e) { PubComputerEventHandler handler = PubComputer; if (handler != null) { handler(this, e); } } protected virtual void OnPubLife(PubEventArgs e) { PubLifeEventHandler handler = PubLife; if (handler != null) { handler(this, e); } } public void issueComputer(string magazineName,DateTime pubDate) { Console.WriteLine("发行"+magazineName); OnPubComputer(new PubEventArgs(magazineName, pubDate)); } public void issueLife(string magazineName, DateTime pubDate) { Console.WriteLine("发行" + magazineName); OnPubLife(new PubEventArgs(magazineName, pubDate)); } } class Subscriber { private string name; public Subscriber(String name) { this.name = name; } public void Receive(object sender,PubEventArgs e) { Console.WriteLine(e.pubDate+" "+name+"收到了 " + e.magazineName); } } class Program { static void Main(string[] args) { Publisher Pub = new Publisher(); Subscriber zs = new Subscriber("Zhang San"); Pub.PubComputer += new Publisher.PubComputerEventHandler(zs.Receive); Subscriber ls = new Subscriber("Li Si"); Pub.PubComputer += new Publisher.PubComputerEventHandler(ls.Receive); Pub.PubLife += new Publisher.PubLifeEventHandler(ls.Receive); Pub.issueComputer("Computer Magazine",Convert.ToDateTime("2009-7-1")); Pub.issueLife("Life Magazine", Convert.ToDateTime("2009-7-1")); Console.WriteLine(); Console.WriteLine("One year later!"); Pub.PubComputer -= new Publisher.PubComputerEventHandler(ls.Receive); Pub.issueLife("Life Magazine", Convert.ToDateTime("2010-7-1")); Pub.issueComputer("Computer Magazine", Convert.ToDateTime("2010-7-1")); Console.ReadKey(); } } }

你可能感兴趣的:(C# .Net 实例积累)