- 필수 기능
- 시작하기
- 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/stringcomparison
Language: C#
Severity: Warning
Category: Best Practices
The rule “Use StringComparison to compare strings” is critical in ensuring accurate and efficient string comparisons in C#. Using methods like ToUpper()
or ToLower()
or operators like ==
to compare strings can lead to incorrect results due to cultural differences in string representation. Furthermore, these methods can cause unnecessary memory allocation and performance issues.
The importance of this rule lies in its ability to prevent potential bugs and enhance the performance of your code. It ensures that string comparisons are done in a way that respects cultural differences and avoids unnecessary operations.
To adhere to this rule, always use the StringComparison
enumeration when comparing strings. For example, use string.Equals(foo, bar, StringComparison.OrdinalIgnoreCase)
instead of foo.ToUpper() == bar.ToUpper()
. This will perform a case-insensitive, culture-invariant comparison that is also more performant.
// do not use `==` to compare strings
if (foo.ToUpper() == bar.ToUpper())
{
}
// do not use `==` to compare strings
if (foo.ToLower() == bar.ToLower())
{
}
if (foo.Foo() == bar.Bar())
{
}
if (string.Equals(foo, bar, StringComparison.OrdinalIgnoreCase))
{
}