首页 > 其他 > 详细

泛型缓存、静态构造函数

时间:2020-05-04 15:56:15      阅读:41      评论:0      收藏:0      [点我收藏+]
泛型缓存比 字典 效率高好几百倍,因为是jit事先编译好的




/// <summary> /// 每个不同的T都会生成一份不同的副本 /// 适合不同类型,需要缓存一份数据的场景,效率高 /// </summary> /// <typeparam name="T"></typeparam> public class GenericCache<T> { private static string _typeName; static GenericCache() { _typeName = typeof(T).Name; Console.WriteLine($"{typeof(T).Name}"); } public static string GetKey() { return _typeName; } } class Program { static void Main(string[] args) { // 泛型 延迟声明 // 泛型的原理 // object 继承(装、拆箱) for (int i = 0; i < 5; i++) { GenericCache<int>.GetKey(); GenericCache<string>.GetKey(); GenericCache<long>.GetKey(); GenericCache<decimal>.GetKey(); GenericCache<double>.GetKey(); }

  技术分享图片

 

   泛型不能在  webservice\wcf 中使用,发布的服务中的类型都必须是确定的

泛型缓存、静态构造函数

原文:https://www.cnblogs.com/hnzheng/p/12826585.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!