- 필수 기능
- 시작하기
- 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/completed-task-not-null
Language: C#
Severity: Warning
Category: Best Practices
This rule emphasizes the importance of always returning a Task
from asynchronous methods in C#, instead of null
. Returning null
from an async method can lead to NullReferenceException
errors at runtime when the returned task is awaited. This can make debugging more difficult and can lead to unexpected behavior or bugs in your application.
The importance of this rule stems from the fact that it’s a common best practice in asynchronous programming. It ensures that your asynchronous methods always return a valid task that can be awaited, regardless of the execution path of your method. This can make your asynchronous code easier to understand and maintain.
To fix this issue, always return a completed Task
or Task<T>
from your asynchronous methods instead of null
. You can use Task.CompletedTask
to return a completed task without a result, or Task.FromResult(result)
to return a completed task with a result. For example: return Task.FromResult<object>(null);
This will ensure that your asynchronous methods are always returning a valid task that can be awaited.
public class {
Task<object> GetAsync()
{
return null;
}
}
Task<object> GetAsync()
{
return null;
}
Task<object> GetAsync()
{
return Task.FromResult<object>(null);
}