zoukankan      html  css  js  c++  java
  • 泛型缓存、静态构造函数、约束

    泛型缓存比 字典 效率高好几百倍,因为是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(); }

      

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

    常见的泛型约束:

        public class Generic<T,S> where T:Program,AA,new()  // 类约束、接口约束、无参构造函数约束
                                  where S:T   // 类型约束
        {
    
    
        }
        public class Generic1<T, S> where T :struct//值类型约束
        {
    
    
        }
        public class Generic2<T, S> where T : class  // 引用类型约束
        {
    
    
        }
    

      

  • 相关阅读:
    B
    I
    C
    判断线段之间的关系(D
    C
    求矩形的周长(线段树+扫描线) Picture POJ
    面积并+扫描线 覆盖的面积 HDU
    线段树->面积并 Atlantis HDU
    E1. Array and Segments (Easy version)(暴力) && E2. Array and Segments (Hard version)(线段树维护)
    Python File writelines() 方法
  • 原文地址:https://www.cnblogs.com/hnzheng/p/12826585.html
Copyright © 2011-2022 走看看