C\#판 등 대상 이 같은 지 아 닌 지 를 모 으 는 방법
5576 단어 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 과 같 습 니 다.모든 것 을 되 돌려 줍 니 다.다음 그림 에서 보 듯 이:이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
WebView2를 Visual Studio 2017 Express에서 사용할 수 있을 때까지Evergreen .Net Framework SDK 4.8 VisualStudio2017에서 NuGet을 사용하기 때문에 패키지 관리 방법을 packages.config 대신 PackageReference를 사용해야...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.