- 필수 기능
- 시작하기
- 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/constant-expected
Language: C#
Severity: Info
Category: Best Practices
The ConstantExpected
rule in C# static analysis helps make sure constants are used correctly. Using constants incorrectly can lead to unexpected behavior, bugs, or security issues.
Apply the ConstantExpected
attribute to parameters that should be constant within a range. This attribute has optional Min
and Max
parameters for the inclusive lower and upper bounds. The data type of Min
and Max
must match the parameter’s data type.
To avoid violations, make sure the ConstantExpected
attribute works with compatible parameter types and that Min
and Max
values are in the right order. The Min
value must be less than or equal to Max
. Also, Min
and Max
should fit within the parameter type’s limits. For instance, if the parameter is an ‘int’, Min
and Max
should also be ‘int’ and within the ‘int’ range. Following these practices makes your code stronger, easier to maintain, and less error-prone.
// check https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1856
// Violation - value not compatible with parameter type.
static void M1([ConstantExpected(Min = "a")] char val) { }
static void M1([ConstantExpected(Min = "a", Max='b')] int val) { }
// Violation - unsupported type for attribute.
static void M2([ConstantExpected] decimal val) { }
// Violation - Min and Max values are inverted.
static void M3([ConstantExpected(Max = 0, Min = 1)] int val) { }
// Violation - value does not fit within the parameter value bounds.
static void M4([ConstantExpected(Min = long.MinValue)] int val) { }
static void M1([ConstantExpected(Min = 'a')] char val) { }
static void M2(decimal val) { }
static void M3([ConstantExpected(Min = 0, Max = 1)] int val) { }
static void M4([ConstantExpected(Min = int.MinValue)] int val) { }