zoukankan      html  css  js  c++  java
  • C# ==和 != 操作符重载注意事项

    1. 重载其中一个必须两个都要重载。

    2. 不能在方法中使用==或!==运算符;

    代码
     public static bool operator ==(TestRefOperatorEquals te1, TestRefOperatorEquals te2)
            {
                
    /* 以下代码是错误的,因为操作符已被重载
                if (te1==null && te2==null)
                {
                    return true;
                }
                if (te1 !=null )
                {
                    return te1.Equals(te2);
                }
                return false;
                
    */
                
    //正确的方法
                return object.ReferenceEquals(te1, te2);
            }

    3.对于值类型和引用类型的处理是不同的

       

  • 相关阅读:
    算法图解
    Cairo graphics tutorial
    远程对象调用
    异步和多线程的关系
    jQuery调用api
    GTK# tutorial
    DLT
    protobuf入门笔记
    PDO讲解
    数据库练习——分页查询
  • 原文地址:https://www.cnblogs.com/nsharp/p/1834583.html
Copyright © 2011-2022 走看看