Like other structured and non-structured languages, C# uses a number of identifiers and keywords that you must know if you want to program without having to have a manual at your elbow. This tip, excerpted from InformIT, discusses these vital parts of the language.
By submitting your personal information, you agree that TechTarget and its partners may contact you regarding relevant content, products and special offers.
Identifiers are names of various program elements in the code that uniquely identify an element. They are the names of things like variables or fields. They're specified by the programmer and should have names that indicate their purpose.
Keywords are reserved words in the C# language. Because they're reserved, they can't be used as identifiers. Examples of keywords are class, public, or void—they are the names of permanent language elements.
Identifiers are names used to identify code elements. The class name HowdyPartner on Line 7 of Listing 1 is an example of an identifier. Identifiers should be meaningful for their intended purpose. For example, the HowdyPartner program prints the words "Howdy, Partner!" to the console.
The C# character set conforms to Unicode 3.0, Technical Report 15, Annex 7. Unicode is a 16-bit character format designed to represent the many characters sets from all languages worldwide. Any Unicode character can be specified with a Unicode escape sequence, u or U, followed by four hex digits. For example, the Unicode escape sequence u0043u0023 represents the characters C#.
The decision to make the C# character set conform to Unicode standards is significant. The most prevalent character set among languages has been the American Standard Code for Information Interchange (ASCII). The primary limitation of ASCII is its 8-bit character size. This doesn't accommodate multibyte character sets for various international languages. Languages such as Java were designed with the Unicode character set built-in. As the world becomes smaller, international considerations must become larger.
Identifiers can have nearly any name, but a few restrictions apply. Here are some rules to follow when creating identifiers:
Here are a few examples of legal C# identifiers:
currentBid _token @override u0043sharp
Now for a few examples of invalid identifiers:
1twothree // error – 1st letter is a number decimal // error – reserved word u0027format // error – Unicode formatting character
The first line is invalid because its first character is a number, which is not allowed. The first character of an identifier must be either a letter character or an underscore. The second identifier is invalid because it is a keyword. C# keywords are reserved and cannot be used as identifiers. The exception is when the keyword is prefixed with the @ character. The third line is invalid because the first character is a Unicode formatting character. Unicode formatting characters are not allowed in any part of an identifier.
Keywords are words reserved by the system and have special predefined meanings when writing C# programs. The class keyword, for instance, is used to define a C# class. Another example is the void keyword, which means that a method does not return a value. These are words that are part of the language itself. Usage of keywords in any context other than what they are defined for in the C# language is likely to make code unreadable. This is the primary reason why keywords are reserved. They are meant to be used only for constructs that are part of the language.
To read the entire article from which this tip is excerpted, click over to InformIT. You have to register there, but the registration is free.