Unlocking the Power of C#: Understanding Keywords and Identifiers
The Building Blocks of C# Programming
In the world of C# programming, keywords and identifiers play a crucial role in creating efficient and effective code. Keywords are predefined sets of reserved words that hold special meaning in a program, while identifiers are the names given to variables, methods, classes, and other entities.
The Unchangeable Nature of Keywords
Keywords in C# cannot be changed or used as identifiers in a program. For instance, the keyword “long” is used to declare variables of type long, and its function cannot be altered. Similarly, keywords like “int”, “char”, and others cannot be used as identifiers. C# has a total of 79 keywords, all of which are in lowercase.
Contextual Keywords: The Exceptions
However, C# also has 25 contextual keywords that have specific meanings in limited program contexts. These keywords can be used as identifiers outside of their designated context and are not reserved words in C#. To learn more about the functions of each keyword, explore the official C# documentation.
The Power of Identifiers
Identifiers, on the other hand, are tokens in a program that uniquely identify an element. They can be names given to variables, methods, classes, and more. For example, “value” is an identifier in the statement “int value = 10;”. Reserved keywords cannot be used as identifiers unless prefixed with the “@” symbol.
The Rules of Naming Identifiers
When it comes to naming identifiers, there are certain rules to follow:
- An identifier cannot be a C# keyword.
- An identifier must begin with a letter, underscore, or “@” symbol.
- The remaining part of the identifier can contain letters, digits, and underscore symbols.
- Whitespaces are not allowed, nor can symbols other than letters, digits, and underscores be used.
- Identifiers are case-sensitive, meaning “getName”, “GetName”, and “getname” represent three different identifiers.
Real-World Example: Finding Keywords and Identifiers
Let’s take a closer look at a simple C# program to understand the concept of keywords and identifiers. In the “Hello World!” program, “Hello World!” is a string literal, while “WriteLine” is an identifier. Can you identify the keywords and identifiers in this program?