C# Keywords

Array Class Collections File Keyword String .NET ASP.NET Cast Compression Data Delegate Directive Enum Exception If Interface LINQ Loop Method Number Process Property Regex Sort StringBuilder Struct Switch Time Windows WPF

Keyword: keywords direct program behavior

Keywords. To convey meaning,
a language has grammar,
syntax
and words. A programming language too has words: these are keywords. Along with syntax, we use keywords to create meaning.


Examples

A list. Each keyword is listed in alphabetical order. These are only the reserved keywords. The list was taken from the C# Programming Language Specification third edition.

abstractasbaseboolbreakbytecasecatchcharcheckedclassconstcontinuedecimaldefaultdelegatedodoubleelseenumeventexplicitexternfalsefinallyfixedfloatforforeachgotoifimplicitinintinterfaceinternalislocklongnamespacenewnullobjectoperatoroutoverrideparamsprivateprotectedpublicreadonlyrefreturnsbytesealedshortsizeofstackallocstaticstringstructswitchthisthrowtruetrytypeofuintulonguncheckedunsafeushortusingvirtualvoidvolatilewhile
Key: used to access value

Reserved keywords cannot be used as variable identifiers or member names. They are part of the language grammar that is used to parse programs. They are at the language's core.


Select method call

Contextual. Some keywords are only considered keywords when used in certain contexts (query expressions). Contextual keywords are still usable as identifiers, unlike reserved keywords.

Contextual
Private keyword

Accessibility. Programs become fantastically complex creatures. The principle of information hiding allows us to prevent certain parts of a program from accessing other parts.


Squares

Accessibility is specified with a modifier on a class member. These keywords also can be used with static members.
The default,
and usually the best,
accessibility is private.

publicprotectedinternalprotected internalprivate
Programming tip

Information hiding can make a program of unmanageable complexity manageable. It introduces synthetic barriers. This reduces the possibility of spaghetti code and needlessly involved logic.

Spaghetti Code
Semicolon

Tokens. Programs are not compiled in a single step. They are first converted into tokens. We explore the concept of tokens. They are understood at the lexical level.

Token

Many languages have common keywords. Often we find the first language we learn is hardest. We use a for-loop in C++ the same as in C# or Java. Some keywords (unsafe) are language-specific.


Note

Quirks. Languages have quirks. An if-statement in C# has slight differences to one in C++. This keeps things interesting.
Yet meaning,
not syntax,
is most important in development.


Keywords are essential to understanding the C# language. On the surface, they seem to be the entire language. But the intricate interactions between them is a greater part.

C#