C#中泛型方法與泛型介面
阿新 • • 發佈:2019-02-07
http://blog.csdn.net/aladdinty/article/details/3486532
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace 泛型
- {
- class 泛型介面
- {
- publicstaticvoid Main()
- {
- PersonManager man = new PersonManager();
- Person per = new Person();
-
man.PrintYourName(per);
- Person p1 = new Person();
- p1.Name = "p1";
- Person p2 = new Person();
- p2.Name = "p2";
- man.SwapPerson<Person>(ref p1, ref p2);
- Console.WriteLine( "P1 is {0} , P2 is {1}" , p1.Name ,p2.Name);
- Console.ReadLine();
-
}
- }
- //泛型介面
- interface IPerson<T>
- {
- void PrintYourName( T t);
- }
- class Person
- {
- publicstring Name = "aladdin";
- }
- class PersonManager : IPerson<Person>
- {
- #region IPerson<Person> 成員
- publicvoid PrintYourName( Person t )
- {
-
Console.WriteLine(
- }
- #endregion
- //交換兩個人,哈哈。。這世道。。
- //泛型方法T型別作用於引數和方法體內
- publicvoid SwapPerson<T>( ref T p1 , ref T p2)
- {
- T temp = default(T) ;
- temp = p1;
- p1 = p2;
- p2 = temp;
- }
- }
- }