C#中泛型方法与泛型接口
来源:互联网 发布:node.js 与spring 编辑:程序博客网 时间:2024/05/29 09:13
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace 泛型
- {
- class 泛型接口
- {
- public static void 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
- {
- public string Name = "aladdin";
- }
- class PersonManager : IPerson<Person>
- {
- #region IPerson<Person> 成员
- public void PrintYourName( Person t )
- {
- Console.WriteLine( "My Name Is {0}!" , t.Name );
- }
- #endregion
-
-
- public void SwapPerson<T>( ref T p1 , ref T p2)
- {
- T temp = default(T) ;
- temp = p1;
- p1 = p2;
- p2 = temp;
- }
- }
- }