Section 1. Introduction. 1.7 C# identifiers and keywords
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.
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.
There are total 78 reserved keywords in C# as follows:
Keywords in C# is mainly divided into 10 categories as follows:
- 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.
- 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.
- 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.
- 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.
- 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.
- Namespace Keywords: There are total 3 keywords in this category which are used in namespaces. The keywords are: namespace, using, extern.
- 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.
- Conversion Keywords: There are 3 keywords which are used in type conversions. The keywords are: explicit, implicit, operator.
- 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.
- Literal Keywords: There are 2 keywords which are used as literal or constant. The keywords are null, default.
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.
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.