Identifiers are used to describe allocated memory types such as integer, longs, classes, and other types defined by C# or by you, as the developer. The rules for identifiers are simple:
Identifiers can start with any Unicode letter or an underscore.
Identifiers are case-sensitive.
Identifiers must be unique within a particular scope: namespace, class, method, or code block.
Typically, identifiers should be descriptive and use whole words without abbreviations. Your identifiers, whether they are for types or methods, should describe the intent that you have in mind when you develop the type or method. The focus should be on the readability of the code.
| Caution |
Make sure that you do not create methods, fields, or properties that have the same identifiers that differ only in case. If you do this, languages that are not case-sensitive, such as Visual Basic, will have difficulties in using your code. |
Your identifiers become the symbolic name that identifies the memory location of the given type when a declaration is made. If an identifier is used in the namespace/global part of the code, you cannot use it again throughout any scope within that namespace, including child parts.
In addition to identifier names, there is also the issue of naming conventions for identifiers. In C#, there is a defined set of naming conventions and recommendations. C++ programmers are familiar with Hungarian notation, which required that identifiers be prefixed with their type. Ironically, Hungarian notation is no longer a recommended naming convention. The following are the two recommended naming conventions for C#:
Pascal casing: This convention capitalizes the first character of each word, as in int MyInt. Pascal casing is the default convention for all names with the exception of protected instance fields and parameters.
Camel casing: This convention capitalizes the first character of each word except the first word, as in int myInt. This convention is used only for protected instance fields and parameters.
The following example shows the use of identifiers in C#.
//Adds System namespace
using System;
//Defines a new namespace
namespace Client.Chapter_1___Common_Type_System
{
//Defines a new class
class MyMainClass
{
//Defines a static method that serves as an entry point
//for the application
static void Main(string[] args)
{
//Declares an instance of variables
int MyInt = 12345;
long MyLong = MyInt;
short MyShort = (short)MyInt;
My2ndFunction(MyInt);
}
//Defines a public static method
public static int My2ndFunction(int myInt)
{
return myInt;
}
}
}