C# identifiers and keywords

0 0
Read Time:4 Minute, 11 Second

Section 1. Introduction. 1.7 C# identifiers and keywords

C# identifiers

In programming languages, identifiers are used for identification purposes. Or in other words, identifiers are the user-defined name of the program components. In C#, an identifier can be a class name, method name, variable name or label. Identifiers can be short names (like x and y) or more descriptive names (firstName, volume). It is recommended to use descriptive names in order to create understandable and maintainable code.

There are certain valid rules for defining a valid C# identifier.

  • The only allowed characters for identifiers are all alphanumeric characters ([A-Z], [a-z], [0-9]), ‘_’ (underscore).
  • Identifiers should not start with digits ([0-9]). For example “12moons” is a not a valid in C# identifier.
  • Identifiers should not contain white spaces.
  • Identifiers should start with a lowercase letter.
  • Keywords are reserved words predefined to the C# compiler. These keywords cannot be used as identifiers. However, if you want to use these keywords as identifiers, you may prefix the keyword with the @ character.
  • C# identifiers allow Unicode Characters.
  • C# identifiers are case-sensitive – “MyName” and “myname” has different meaning.
  • C# identifiers cannot contain more than 512 characters.
  • Identifiers does not contain two consecutive underscores in its name because such types of identifiers are used for the implementation.

C# keywords

Keywords are reserved words predefined to the C# compiler. These keywords cannot be used as identifiers. Doing this will result in a compile-time error. However, if you want to use these keywords as identifiers, you may prefix the keyword with the @ character.

In C#, some identifiers have special meaning in context of code, such as get and set are called contextual keywords.

Reserved keywords

There are total 78 reserved keywords in C# as follows:

Keywords in C# is mainly divided into 10 categories as follows:

  1. Value Type Keywords: There are 15 keywords in value types which are used to define various data types:
    bool, byte, char, decimal, double, enum, float, int, long, sbyte, short, struct, unit, ulong, ushort.
  2. Reference Type Keywords: There are 6 keywords in reference types which are used to store references of the data or objects. The keywords in this category are: class, delegate, interface, object, string, void.
  3. Modifiers Keywords: There are 17 keywords in modifiers which are used to modify the declarations of type member: public, private, internal, protected, abstract, const, event, extern, new, override, partial, readonly, sealed, static, unsafe, virtual, volatile.
  4. Statements Keywords: There are total 18 keywords which are used in program instructions: if, else, switch, do, for, foreach, in, while, break, continue, goto, return, throw, try, catch, finally, checked, unchecked, fixed, case, lock.
  5. Method Parameters Keywords: There are total 4 keywords which are used to change the behavior of the parameters that passed to a method. The keyword includes in this category are: params, in, ref, out.
  6. Namespace Keywords: There are total 3 keywords in this category which are used in namespaces. The keywords are: namespace, using, extern.
  7. Operator Keywords: There are total 8 keywords which are used for different purposes like creating objects, getting a size of object etc. The keywords are: as, is, new, sizeof, typeof, true, false, stackalloc.
  8. Conversion Keywords: There are 3 keywords which are used in type conversions. The keywords are: explicit, implicit, operator.
  9. Access Keywords: There are 2 keywords which are used in accessing and referencing the class or instance of the class. The keywords are base, this.
  10. Literal Keywords: There are 2 keywords which are used as literal or constant. The keywords are null, default.

Contextual Keywords

These are used to give a specific meaning in the program. Whenever a new keyword comes in C#, it is added to the contextual keywords, not in the keyword category. This helps to avoid the crashing of programs which are written in earlier versions.

  • These are not reserved words.
  • It can be used as identifiers outside the context that’s why it named contextual keywords.
  • These can have different meanings in two or more contexts.
  • There are total 30 contextual keywords in C#: add, alias, ascending, async, await, by, descending, dynamic, equals, from, get, global, group, into, join, let, nameof, on, orderby, partial (type), partial (method), remove, select, set, value, var, when, where, where, yield.

For more details check this link.

Wrapping out

We ahev learnt about C# identifiers and keywords.

Thank you for reading.

Check for more tutorials at acoptex.lt.

Check for Arduino and Raspberry Pi projects on our website acoptex.com.

Section 1. Introduction. 1.1 C# programming language

Section 1. Introduction. 1.2 Introduction to .NET Framework

Section 1. Introduction. 1.3 C# versions history

Section 1. Introduction. 1.4 C# vs Java

Section 1. Introduction. 1.5 C# get started

Section 1. Introduction. 1.6 Your first program – Hello world

Section 1. Introduction. 1.7 C# identifiers and keywords

Section 2. Fundamentals. 2.1 C# Comments

Section 2. Fundamentals. 2.2 C# Data types

Section 2. Fundamentals. 2.3 C# Constants and Literals

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

Leave a Reply

Your email address will not be published. Required fields are marked *