literal-string
C# Literal string을 const string으로 사전 정의하여 사용하는 패턴
When & Why to Use This Skill
This Claude skill streamlines C# development by enforcing the best practice of replacing literal strings with predefined 'const string' declarations. It focuses on improving code maintainability, enhancing type safety, and optimizing performance by centralizing string management and eliminating 'magic strings' within the codebase.
Use Cases
- Refactoring hardcoded error messages and notification text into centralized constants to simplify future updates and localization.
- Standardizing string-based keys or identifiers used in conditional logic to prevent bugs caused by typos and improve readability.
- Optimizing application performance by leveraging string literal deduplication through the use of constant definitions.
- Automating the identification of 'magic strings' during code reviews to ensure compliance with professional C# architectural standards.
| name | literal-string |
|---|---|
| description | "C# Literal string을 const string으로 사전 정의하여 사용하는 패턴" |
Literal String 처리
C# 코드에서 Literal string을 처리하는 방법에 대한 가이드입니다.
규칙
Literal string에 대해서는 가급적 const string으로 사전에 정의하여 사용할 것
예시
좋은 예
// 좋은 예
const string ErrorMessage = "오류가 발생했습니다.";
// An error has occurred.
if (condition)
throw new Exception(ErrorMessage);
나쁜 예
// 나쁜 예
if (condition)
throw new Exception("오류가 발생했습니다.");
이유
- 유지보수성: 메시지 변경 시 한 곳만 수정
- 재사용성: 동일한 메시지를 여러 곳에서 사용 가능
- 타입 안전성: 컴파일 타임에 오타 확인
- 성능: 문자열 리터럴 중복 제거