C#. Keywords in C#. Reserved words. Context words

Keywords in C#. Reserved words. Context words


Contents


1. Definition of keyword

In general, a keyword is a programming language tool that:

  • is a built-in programming language;
  • used to build a program in this language.

In the C# programming language, there are a number of keywords that are used to develop programs.

 

2. What are the common types of keywords defined in C#?

In the C# programming language, two general types of keywords are defined:

  • reserved keywords. These are keywords that are built into the C# language and which are not allowed to be used in the names of variables, constants, methods (functions), classes, etc.;
  • contextual keywords. These are special words that take on a specific meaning depending on the context.

 

3. Reserved keywords. List

The following reserved keywords are used in the C# programming language:

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

 

4. Contextual keywords. List

Context words take on special meaning under certain conditions (specific context). Under these conditions (context), these words have the meaning of reserved words. Outside the context, these words can be used as names for other program elements (variables, constants, classes, etc.). Therefore, contextual words are considered to be formally reserved. However, the use of contextual words outside the context can lead to confusion in the program.



Conclusion: outside the context it is recommended to choose the names of program elements so that they do not coincide with the contextual words.

In C#, the following contextual words are used:

add         dynamic     from       get         global
group       into        join       let         orderby
partial     remove      select     set         value
var         where       yield

 

5. In what elements of the programming language is it forbidden to use keywords as names? Example

When writing programs, the programmer encounters the task of choosing a name for a variable, method, class, etc. The choice of name depends on the purpose of the variable, method, class, etc. However, in C# (also in other programming languages) there are restrictions on the choice of a name. It is allowed to choose any variable names, except those names that are keywords.

Keywords are not allowed in names of:

  • variables (instances or objects of a class);
  • constants;
  • methods;
  • classes;
  • structures;
  • enumerations.

Example. If the program tries to declare an enumeration with the name int

...
enum int // this is error!
{
    Mon = 1, Tue
}
...

then the compiler will generate an error

Error  1       Identifier expected; 'int' is a keyword

This is logical, since the name int is a keyword that defines an integer data type.

 


Related topics