zoukankan      html  css  js  c++  java
  • error? in CLR via c#

    the book said:

    Comparing Two Generic Type Variables with Each Other
    Comparing two variables of the same generic type is illegal if the generic type parameter is not
    known to be a reference type.
    private static void ComparingTwoGenericTypeVariables<T>(T o1, T o2) {
    if (o1 == o2) { } // Error
    }
    In this example, T is unconstrained, and whereas it is legal to compare two reference type vari-
    ables with one another, it is not legal to compare two value type variables with one another unless
    the value type overloads the == operator. If T were constrained to class , this code would compile,
    and the == operator would return true if the variables referred to the same object, checking for
    exact identity. Note that if T were constrained to a reference type that overloaded the operator ==
    method, the compiler would emit calls to this method when it sees the == operator. Obviously, this
    whole discussion applies to uses of the != operator too.

    try

    void Main()
    {
        Test.test<B>(new B(), new B());
    }
    
    // Define other methods and classes here
    class A { 
        public static bool operator==(A x, A y) { 
            return true; 
        }
        public static bool operator!=(A x, A y) { 
            return false; 
        }
    }
    class B : A { 
        public static bool operator==(B x, B y) { 
            return false; 
        }
        public static bool operator!=(B x, B y) { 
            return true; 
        }
    }
    class Test { 
        public static void test<T>(T a, T b) where T : class { 
            Console.WriteLine(a == b); 
        } 
    }

    result:false

    IL:

    Test.test:
    IL_0000:  nop         
    IL_0001:  ldarg.0     
    IL_0002:  box         01 00 00 1B 
    IL_0007:  ldarg.1     
    IL_0008:  box         01 00 00 1B 
    IL_000D:  ceq         
    IL_000F:  call        System.Console.WriteLine
    IL_0014:  nop         
    IL_0015:  ret         

    but

    public static void test<T>(T a, T b) where T : A { 
    Test.test:
    IL_0000:  nop         
    IL_0001:  ldarg.0     
    IL_0002:  box         01 00 00 1B 
    IL_0007:  ldarg.1     
    IL_0008:  box         01 00 00 1B 
    IL_000D:  call        UserQuery+A.op_Equality
    IL_0012:  call        System.Console.WriteLine
    IL_0017:  nop         
    IL_0018:  ret  

     

  • 相关阅读:
    GhostBSD 3.0RC3,基于GNOME的FreeBSD
    Nagios 3.4.3 发布,企业级监控系统
    Jolokia 1.0.6 发布, JMX远程访问方法
    微软希望开发人员不要使 WebKit 成为新版 IE6
    Kwort Linux 3.5 正式版发布
    EJDB 1.0.24 发布,嵌入式 JSON 数据库引擎
    Pale Moon 15.3 Firefox“苍月”优化版发布
    Galera Load Balancer 0.8.1 发布
    SmartSVN V7.5 正式发布
    PostgresQL建立索引如何避免写数据锁定
  • 原文地址:https://www.cnblogs.com/xray/p/5732219.html
Copyright © 2011-2022 走看看