- 필수 기능
- 시작하기
- Glossary
- 표준 속성
- Guides
- Agent
- 통합
- 개방형텔레메트리
- 개발자
- Administrator's Guide
- API
- Datadog Mobile App
- CoScreen
- Cloudcraft
- 앱 내
- 서비스 관리
- 인프라스트럭처
- 애플리케이션 성능
- APM
- Continuous Profiler
- 스팬 시각화
- 데이터 스트림 모니터링
- 데이터 작업 모니터링
- 디지털 경험
- 소프트웨어 제공
- 보안
- AI Observability
- 로그 관리
- 관리
",t};e.buildCustomizationMenuUi=t;function n(e){let t='
",t}function s(e){let n=e.filter.currentValue||e.filter.defaultValue,t='${e.filter.label}
`,e.filter.options.forEach(s=>{let o=s.id===n;t+=``}),t+="${e.filter.label}
`,t+=`ID: csharp-best-practices/reference-equals-value-types
Language: C#
Severity: Warning
Category: Best Practices
The Object.ReferenceEquals
method in C# is used to determine if two object references refer to the same object, not if the objects themselves are equal. This rule is important because value types in C# are not reference types. They are stored on the stack, not the heap, and each instance has its own copy of the data. Therefore, using Object.ReferenceEquals
with value types will always return false because they are boxed into separate objects on the heap.
To avoid breaking this rule, you should not use Object.ReferenceEquals
to compare value types. Instead, you can use the ==
operator or Object.Equals
method, which are designed to compare the values of two objects, not their references. This is the correct way to compare value types in C#, and it will give you the expected behavior.
For example, instead of writing Object.ReferenceEquals(int1, int2)
, you should write int1 == int2
or Object.Equals(int1, int2)
. These expressions will correctly compare the values of int1
and int2
, not their references.
int int1 = 1
int int2 = 1;
Console.WriteLine(Object.ReferenceEquals(int1, int2));
int int1 = 1, int2 = 1;
Console.WriteLine(Object.ReferenceEquals(int1, int2));
int int1 = 1, int2 = 1;
Console.WriteLine(int1 == int2);
Console.WriteLine(object.Equals(int1, int2));