C# 키워드

C# 키워드 (keywords)

C#을 처음 배울 때, 코드를 읽다 보면 public, class, void 같은 낯선 단어들이 반복해서 등장해요. 이렇게 컴파일러가 특별한 의미로 미리 정해 둔 예약식별자(reserved identifier)를 키워드라고 부르죠. 프로그램에서 이 단어들을 식별자(이름)로 그냥 쓰면 안 돼요. 대신 @를 앞에 붙이면 가능해요. 예를 들어 @if는 유효한 식별자지만, if는 키워드라서 그대로 이름으로 쓸 수 없어요.

출처: C# Keywords and contextual keywords (Microsoft Learn)

본문

이 문서의 첫 번째 표는 C# 프로그램 어디에서든 예약식별자로 취급되는 키워드들을, 두 번째 표는 C#의 상황별 키워드(contextual keywords) 를 나열한 거예요. 상황별 키워드는 아주 한정된 문맥에서만 특별한 의미를 갖고, 그 밖의 자리에서는 식별자로 써도 돼요. 언어에 새 키워드가 추가될 때는 이전 버전에서 작성된 프로그램을 깨지 않도록 대부분 상황별 키워드로 추가돼요.

예약 키워드

abstract event namespace static
as explicit new string
base extern null struct
bool false object switch
break finally operator this
byte fixed out throw
case float override true
catch for params try
char foreach private typeof
checked goto protected uint
class if public ulong
const implicit readonly unchecked
continue in ref unsafe
decimal int return ushort
default interface sbyte using
delegate internal sealed virtual
do is short void
double lock sizeof volatile
else long stackalloc while
enum

상황별 키워드 (Contextual keywords)

상황별 키워드는 코드 안에서 특정한 의미를 주지만, C#의 예약어는 아니에요. partial이나 where처럼 몇몇 상황별 키워드는 두 가지 이상의 문맥에서 저마다 다른 의미를 갖기도 해요.

add extension nint scoped
allows field not select
alias file notnull set
and from nuint unmanaged (function pointer calling convention)
ascending get on unmanaged (generic type constraint)
args global or value
async group orderby var
await init partial (type) when (filter condition)
by into partial (member) where (generic type constraint)
closed join record where (query clause)
descending let remove with
dynamic managed (function pointer calling convention) required yield
equals nameof safe

더 알아보기

  • C# reference (Microsoft Learn)
  • 이 문서는 원문에서 가져온 키워드 목록과 링크를 그대로 보존했어요. 각 키워드의 상세 설명은 개별 링크를 따라가면 확인할 수 있어요.