1 引入泛型:延迟声明 2 如何声明和使用泛型 3 泛型的好处和原理 4 泛型类、泛型方法、泛型接口、泛型委托 5 泛型约束 6 协变 逆变(选修) 7 泛型缓存(选修)
List就是泛型,为什么要有泛型? List是一个集合,可能是一组int 也可能是一组string 泛型就是用一个东西来满足多种不同类型的需求的
1 任何父类出现的地方,都可以用子类来代替 2 object是一切类型的父类
object类型参数有2个问题: 1 装箱拆箱,性能损耗 传入一个int值(栈) object又在堆里面,如果把int传递进来,就会把值从栈里面copy到堆里 使用的时候,又需要用对象值,又会copy到栈(拆箱)
2 类型安全问题,可能会有,因为传递的对象是没有限制的
Model.cs
namespace MyGeneric
{
public interface ISports
{
void Pingpang();
}
public interface IWork
{
void Work();
}
public class People
{
public int Id { get; set; }
public string Name { get; set; }
public void Hi()
{ }
}
public class Chinese : People, ISports, IWork
{
public void Tradition()
{
Console.WriteLine("仁义礼智信,温良恭俭让");
}
public void SayHi()
{
Console.WriteLine("吃了么?");
}
public void Pingpang()
{
Console.WriteLine("打乒乓球...");
}
public void Work()
{
throw new NotImplementedException();
}
}
public class Hubei : Chinese
{
public string Changjiang { get; set; }
public void Majiang()
{
Console.WriteLine("打麻将啦。。");
}
}
public class Japanese : ISports
{
public int Id { get; set; }
public string Name { get; set; }
public void Pingpang()
{
Console.WriteLine("打乒乓球...");
}
}
}
Monitor.cs
namespace MyGeneric
{
public class Monitor
{
public static void Show()
{
Console.WriteLine("****************Monitor******************");
{
int iValue = 12345;
long commonSecond = 0;
long objectSecond = 0;
long genericSecond = 0;
{
Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 0; i < 100_000_000; i++)
{
ShowInt(iValue);
}
watch.Stop();
commonSecond = watch.ElapsedMilliseconds;
}
{
Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 0; i < 100_000_000; i++)
{
ShowObject(iValue);
}
watch.Stop();
objectSecond = watch.ElapsedMilliseconds;
}
{
Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 0; i < 100_000_000; i++)
{
Show<int>(iValue);
}
watch.Stop();
genericSecond = watch.ElapsedMilliseconds;
}
Console.WriteLine("commonSecond={0},objectSecond={1},genericSecond={2}"
, commonSecond, objectSecond, genericSecond);
}
}
#region PrivateMethod
private static void ShowInt(int iParameter)
{
}
private static void ShowObject(object oParameter)
{
}
private static void Show<T>(T tParameter)
{
}
#endregion
}
}
GenericTest.cs
namespace MyGeneric
{
public class GenericTest
{
public static void Show<T>(T tParameter)
{
Console.WriteLine("This is {0},parameter={1},type={2}",
typeof(CommonMethod), tParameter.GetType().Name, tParameter);
}
}
public class GenericClass<T>
{
}
public class ChildClass<S, T> : GenericClass<S>, GenericInterface<T>
{
}
public class ChildClass1 : GenericInterface<int>
{
}
public interface GenericInterface<T>
{
}
public delegate void Do<T>();
}
CommonMethod.cs
namespace MyGeneric
{
public class CommonMethod
{
public static void ShowInt(int iParameter)
{
Console.WriteLine("This is {0},parameter={1},type={2}",
typeof(CommonMethod).Name, iParameter.GetType().Name, iParameter);
}
public static void ShowString(string sParameter)
{
Console.WriteLine("This is {0},parameter={1},type={2}",
typeof(CommonMethod).Name, sParameter.GetType().Name, sParameter);
}
public static void ShowDateTime(DateTime dtParameter)
{
Console.WriteLine("This is {0},parameter={1},type={2}",
typeof(CommonMethod).Name, dtParameter.GetType().Name, dtParameter);
}
public static void ShowObject(object oParameter)
{
Console.WriteLine("This is {0},parameter={1},type={2}",
typeof(CommonMethod), oParameter.GetType().Name, oParameter);
}
public static void Show<T>(T tParameter)
{
Console.WriteLine("This is {0},parameter={1},type={2}",
typeof(CommonMethod), tParameter.GetType().Name, tParameter);
}
}
}
GenericConstraint.cs
namespace MyGeneric
{
public class GenericConstraint
{
public static void ShowObject(object oParameter)
{
People people = (People)oParameter;
Console.WriteLine(people.Id);
Console.WriteLine("This is {0},parameter={1},type={2}",
typeof(CommonMethod), oParameter.GetType().Name, oParameter);
}
public static void Show<T>(T tParameter)
where T : ISports
{
tParameter.Pingpang();
Console.WriteLine("This is {0},parameter={1},type={2}",
typeof(CommonMethod), tParameter.GetType().Name, tParameter);
}
public static void GenericShow<T>(T tParameter)
{
Console.WriteLine("This is {0},parameter={1},type={2}",
typeof(CommonMethod), tParameter.GetType().Name, tParameter);
}
public static void GenericShow1<T>(T tParameter)
where T : class, new()
{
Console.WriteLine("This is {0},parameter={1},type={2}",
typeof(CommonMethod), tParameter.GetType().Name, tParameter);
}
public static T GenericShow2<T>()
{
return default(T);
}
}
}
Program.cs
namespace MyGeneric
{
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine(typeof(List<>));
Console.WriteLine(typeof(Dictionary<,>));
Console.WriteLine("欢迎来到.net高级班vip课程,今天是Richard老师给大家带来的泛型Generic");
int iValue = 123;
string sValue = "456";
DateTime dtValue = DateTime.Now;
object oValue = "789";
Console.WriteLine("***********************Common***********************");
CommonMethod.ShowInt(iValue);
CommonMethod.ShowString(sValue);
CommonMethod.ShowDateTime(dtValue);
Console.WriteLine("***********************Object***********************");
CommonMethod.ShowObject(oValue);
CommonMethod.ShowObject(iValue);
CommonMethod.ShowObject(sValue);
CommonMethod.ShowObject(dtValue);
Console.WriteLine("***********************Generic***********************");
CommonMethod.Show(iValue);
// 因为类型错了
CommonMethod.Show(sValue);
CommonMethod.Show<DateTime>(dtValue);
CommonMethod.Show<object>(oValue);
Console.WriteLine("***********************GenericCache***********************");
Console.WriteLine("***********************GenericConstraint***********************");
People people = new People()
{
Id = 123,
Name = "Richard"
};
Chinese chinese = new Chinese()
{
Id = 234,
Name = "习大大"
};
Hubei hubei = new Hubei()
{
Id = 345,
Name = "王市长"
};
Japanese japanese = new Japanese()
{
Id = 678,
Name = "福原爱"
};
GenericConstraint.GenericShow(people);
GenericConstraint.GenericShow(chinese);
GenericConstraint.GenericShow(hubei);
GenericConstraint.GenericShow(japanese);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.Read();
}
}
}
选修: GenericCacheTest.cs
namespace MyGeneric.Extend
{
public class GenericCacheTest
{
public static void Show()
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine(GenericCache<int>.GetCache());
Thread.Sleep(10);
Console.WriteLine(GenericCache<long>.GetCache());
Thread.Sleep(10);
Console.WriteLine(GenericCache<DateTime>.GetCache());
Thread.Sleep(10);
Console.WriteLine(GenericCache<string>.GetCache());
Thread.Sleep(10);
Console.WriteLine(GenericCache<GenericCacheTest>.GetCache());
Thread.Sleep(10);
}
}
}
public class DictionaryCache
{
private static Dictionary<Type, string> _TypeTimeDictionary = null;
static DictionaryCache()
{
Console.WriteLine("This is DictionaryCache 静态构造函数");
_TypeTimeDictionary = new Dictionary<Type, string>();
}
public static string GetCache<T>()
{
Type type = typeof(Type);
if (!_TypeTimeDictionary.ContainsKey(type))
{
_TypeTimeDictionary[type] = string.Format("{0}_{1}", typeof(T).FullName, DateTime.Now.ToString("yyyyMMddHHmmss.fff"));
}
return _TypeTimeDictionary[type];
}
}
public class GenericCache<T>
{
private static string _TypeTime = "";
static GenericCache()
{
Console.WriteLine("This is GenericCache 静态构造函数");
_TypeTime = string.Format("{0}_{1}", typeof(T).FullName, DateTime.Now.ToString("yyyyMMddHHmmss.fff"));
}
public static string GetCache()
{
return _TypeTime;
}
}
}
CCTest.cs
namespace MyGeneric.Extend
{
public class CCTest
{
public static void Show()
{
{
Bird bird1 = new Bird();
Bird bird2 = new Sparrow();
Sparrow sparrow1 = new Sparrow();
;
}
{
List<Bird> birdList1 = new List<Bird>();
List<Bird> birdList3 = new List<Sparrow>().Select(c => (Bird)c).ToList();
}
{
IEnumerable<Bird> birdList1 = new List<Bird>();
IEnumerable<Bird> birdList2 = new List<Sparrow>();
ICustomerListOut<Bird> customerList1 = new CustomerListOut<Bird>();
ICustomerListOut<Bird> customerList2 = new CustomerListOut<Sparrow>();
}
{
ICustomerListIn<Sparrow> customerList2 = new CustomerListIn<Sparrow>();
ICustomerListIn<Sparrow> customerList1 = new CustomerListIn<Bird>();
customerList1.Show(new Sparrow());
ICustomerListIn<Bird> birdList1 = new CustomerListIn<Bird>();
birdList1.Show(new Sparrow());
birdList1.Show(new Bird());
}
{
IMyList<Sparrow, Bird> myList1 = new MyList<Sparrow, Bird>();
IMyList<Sparrow, Bird> myList2 = new MyList<Sparrow, Sparrow>();
IMyList<Sparrow, Bird> myList3 = new MyList<Bird, Bird>();
IMyList<Sparrow, Bird> myList4 = new MyList<Bird, Sparrow>();
}
}
}
public class Bird
{
public int Id { get; set; }
}
public class Sparrow : Bird
{
public string Name { get; set; }
}
public interface ICustomerListIn<in T>
{
void Show(T t);
}
public class CustomerListIn<T> : ICustomerListIn<T>
{
public void Show(T t)
{
}
}
public interface ICustomerListOut<out T>
{
T Get();
}
public class CustomerListOut<T> : ICustomerListOut<T>
{
public T Get()
{
return default(T);
}
}
public interface IMyList<in inT, out outT>
{
void Show(inT t);
outT Get();
outT Do(inT t);
}
public class MyList<T1, T2> : IMyList<T1, T2>
{
public void Show(T1 t)
{
Console.WriteLine(t.GetType().Name);
}
public T2 Get()
{
Console.WriteLine(typeof(T2).Name);
return default(T2);
}
public T2 Do(T1 t)
{
Console.WriteLine(t.GetType().Name);
Console.WriteLine(typeof(T2).Name);
return default(T2);
}
}
}
|