C\#판 등 대상 이 같은 지 아 닌 지 를 모 으 는 방법

5576 단어 C#
본 고 는 사례 형식 으로 C\#판 등 대상 이 똑 같은 지 여 부 를 보 여 주 었 고 매우 실 용적 이 며 참고 할 수 있 습 니 다.구체 적 인 분석 은 다음 과 같다.
1.판단 이 같은 3 가지 방법
1.실례 방법

public virtual bool Equals(object obj) 
{ 
  return RuntimeHelpers.Equals(this, obj); 
}

2.비교 값 유형 정적 방법

public static bool Equals(object objA, object objB) 
{ 
  return ((objA == objB) || (((objA != null) && (objB != null)) && objA.Equals(objB))); 
}

3.참조 형식 정적 방법 비교

public static bool ReferenceEquals(object objA, object objB) 
{ 
  return (objA == objB); 
}

2.인용 유형 이 동일 한 지 판단

  class Program 
  { 
    static void Main(string[] args) 
    { 
      Team t1 = new Team("    ・  "); 
      Team t2 = new Team("    ・  "); 
      var result = (t1 == t2); 
      Console.WriteLine(result); 
      result = t1.Equals(t2); 
      Console.WriteLine(result); 
      Console.ReadKey(); 
    } 
  }
 
  public class Team 
  { 
    public string _coach = string.Empty;
 
    public Team(string coach) 
    { 
      this._coach = coach; 
    } 
  }
 
  public struct TeamStruct 
  { 
    public string _coach;
 
    public TeamStruct(string coach) 
    { 
      this._coach = coach; 
    } 
  }
 
실행 결과:
false
false
분석:인용 유형 은 인용 주 소 를 비교 합 니 다.t1 과 t2 가 서로 다른 대상 인 스 턴 스 를 가리 키 기 때문에 dou 는 모두 false 로 돌아 갑 니 다.  
3.값 유형 이 같 는 지 판단 합 니 다.
1.값 유형 판단 방법
System.ValueType 에서 파생 되 어 System.Object 의 가상 방법 Equals(object obj)를 재 작성 하 였 습 니 다.

public override bool Equals(object obj) 
{ 
  if (obj == null) 
  { 
    return false; 
  } 
  RuntimeType type = (RuntimeType) base.GetType(); 
  RuntimeType type2 = (RuntimeType) obj.GetType(); 
  if (type2 != type) //              
  { 
    return false; 
  } 
  object a = this; 
  if (CanCompareBits(this)) //                false 
  { 
    return FastEqualsCheck(a, obj); 
  } 
  //             
  FieldInfo[] fields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); 
  for (int i = 0; i < fields.Length; i++) //    ,          
  { 
    object obj3 = ((RtFieldInfo) fields[i]).UnsafeGetValue(a); 
    object obj4 = ((RtFieldInfo) fields[i]).UnsafeGetValue(obj); 
    if (obj3 == null) 
    { 
      if (obj4 != null) 
      { 
        return false; 
      } 
    } 
    else if (!obj3.Equals(obj4)) 
    { 
      return false; 
    } 
  } 
  return true; 
}

2.용==동일 여 부 를 판단 한다

    static void Main(string[] args) 
    { 
      TeamStruct ts1 = new TeamStruct("    ・  "); 
      TeamStruct ts2 = ts1; 
      var result = (ts1 == ts2); 
      Console.WriteLine(result); 
      Console.ReadKey(); 
    }
컴 파일 오류 가 발생 했 습 니 다.값 유형 이==로 판단 할 수 없 기 때문이다.
3.Equals()인 스 턴 스 방법 으로 동일 여 부 를 판단 한다.

    static void Main(string[] args) 
    { 
      TeamStruct ts1 = new TeamStruct("    ・  "); 
      TeamStruct ts2 = ts1; 
      var result = ts1.Equals(ts2); 
      Console.WriteLine(result); 
      Console.ReadKey(); 
    }

트 루 로 돌아 가기.
값 형식의 필드 가 같 으 면 같다 는 것 을 알 수 있다.

    static void Main(string[] args) 
    { 
      TeamStruct ts1 = new TeamStruct("    ・  "); 
      TeamStruct ts2 = ts1; 
      ts2._coach = "   "; 
      var result = ts1.Equals(ts2); 
      Console.WriteLine(result); 
      Console.ReadKey(); 
    }

false 로 돌아 갑 니 다.물론 값 형식의 필드 가 같 지 않 으 면 전체 가 같 지 않 습 니 다.
4.복잡 치 유형 이 동일 한 지 판단
즉,값 형식 에는 참조 형식 과 값 형식 이 포함 되 어 있 습 니 다.

  class Program 
  { 
    static void Main(string[] args) 
    { 
      Team t = new Team("    ・  "); 
      TeamStruct ts = new TeamStruct("    ・  ");
 
      NationalTeam nt1 = new NationalTeam(t, ts); 
      NationalTeam nt2 = nt1; 
      var result = nt1.Equals(nt2); 
      Console.WriteLine(result); 
      Console.ReadKey(); 
    } 
  }
 
  public class Team 
  { 
    public string _coach = string.Empty;
 
    public Team(string coach) 
    { 
      this._coach = coach; 
    } 
  }
 
  public struct TeamStruct 
  { 
    public string _coach;
 
    public TeamStruct(string coach) 
    { 
      this._coach = coach; 
    } 
  }
 
  public struct NationalTeam 
  { 
    public Team _team; 
    public TeamStruct _structTeam;
 
    public NationalTeam(Team team, TeamStruct structTeam) 
    { 
      this._team = team; 
      this._structTeam = structTeam; 
    } 
  }
 
트 루 로 돌아 가면 인용 형식 구성원 과 값 형식 구성원 을 비교 합 니 다.nt1 과 nt2 에서 유형 은 Team 의 인용 유형 멤버team 이 같은 대상 을 가리 키 는 실례,  타 입 TeamStruct 의 값 타 입 멤버structTeam 과 같 습 니 다.모든 것 을 되 돌려 줍 니 다.다음 그림 에서 보 듯 이:

좋은 웹페이지 즐겨찾기