Pages

Thursday, 11 June 2015

Identifier and Keywords

Identifier

Identifiers are character strings used to name things such as class, variables, methods, parameters and so on. You can create self-documenting identifiers by concatenating meaningful words into a single descriptive name, using uppercase and lowercase letters (e.g., FirstName, LastName, SocialSecurityNum, etc.). These are the identifiers in previous example program:
System Program1 Program Main args Console WriteLine Name ReadLine Read
There's some rule for defining identifier:
  • Identifiers are case-sensitive. For instance, the variable names myVariable and myvariable are different identifiers.
  • The alphabetic and underscore characters (a through z, A through Z, and _) are allowed at any position.
  • Digits are not allowed in the first position but are allowed everywhere else. Therefore: person1 is legal identifier, but 1Person is illegal
  • The @ character is allowed in the first position of an identifier but not at any other position. Although allowed, its use is generally discouraged. @FirstName is legal but First@name will be resulting compile error.

Commonly Used Identifier Naming Styles

Style Name Description Recommended Use Examples
Pascal casing Each word in the identifier is capitalized. Use for type names and member names visible outside the class. These include the names of the following: classes, methods, namespaces, properties, and public fields. FirstName,LastName
Camel casing Each word in the identifier, except the first, is capitalized. Use for local variable names and the names of formal parameters in method declarations. firstName,lastName
Camel case with leading underscore This is a camel-cased identifier that starts with an underscore. Use for the names of private and protected fields. _firstName,_lastName

Keywords

Keywords are the character string tokens used to define the C# language. The important thing about keyword is that they can not be used as identifier.
abstract const extern int out short typeof
as continue false interface override sizeof uint
base decimal finally internal params stackalloc ulong
bool default fixed is private static unchecked
break delegate float lock protected string unsafe
byte do for long public struc ushort
case double foreach namespace readonly switch using
catch else goto new ref this virtual
char enum if null return throw void
checked event implicit object sbyte true volatile
class explicit in operator sealed try while
Keywords cannot be used as variable names or any other form of identifier, unless prefaced with the @ character.

Contextual keywords are identifiers that act as keywords only in certain language constructs, this mean we can still use this as identifier.
add ascending async awai by descending dynamic
equals from get global group in into
join let on orderby partial remove select
set value var where yield

Main Method

Every C# program must have one class with a method called Main. In program shown previously, it was declared in a class called Program.
  • The starting point of execution of every C# program is at the first instruction in Main.
  • The name Main must be capitalized.
The simplest form of Main is the following:
static void Main( )
{
    Statements
}

0 comments:

Post a Comment