代码片断

///<summary>/// override 系统的Equals函数
        ///</summary>///<param name="obj"></param>///<returns>返回是否相等</returns>publicoverride Boolean Equals(Object obj)
        {
            if (this== obj)
                returntrue;
            if (obj ==null)
                returnfalse;
            if (GetType() != obj.GetType())
                returnfalse;
            aprResult other = (aprResult)obj;
            if (this.numA == other.numA &&this.numB == other.numB)

                returntrue;
            returnfalse;
        }

        ///<summary>/// 重载系统的GetHashCode函数
        ///</summary>///<returns>返回hashcode</returns>publicoverrideint GetHashCode()
        {
            int PRIME =31;
            int result =1;
            result = PRIME * result + ((numA ==0) ?0 : numA.GetHashCode());
            return result;
        }


///////
// 使用asobject o = GetFromCache("KEY");
EmployeeInfo employee = o as EmployeeInfo; // 直接试图去转换,如果不成功,返回null,永远不会抛出异常if(employee !=null) {
  // 有用的代码}

// 使用is(is被调用了2次,类型判断被执行了2次)object o = GetFromCache("KEY");
if(o is EmployeeInfo) { // 这个判断是需要时间的  EmployeeInfo employee = o as EmployeeInfo; // 转换,注意,该方法会再次调用is进行类型判断
  // 有用的代码}
///////


posted on 2011-12-08 10:55  武胜-阿伟  阅读(133)  评论(0编辑  收藏  举报