Pages

Showing posts with label Identifier and Keywords. Show all posts
Showing posts with label Identifier and Keywords. Show all posts

Thursday, 11 June 2015

Block and Statements

Statements

The statements in C# are very similar to those of C and C++. A statement is a source code instruction describing a type or telling the program to perform an action.

A simple statement is terminated by a semicolon. (;)

For example, the following code is a sequence of two simple statements. The first statement defines an integer variable named var1 and initializes its value to 5. The second statement prints the value of variable var1 to a window on the screen.
int var1 = 5;
System.Console.WriteLine("The value of var1 is {0}", var1);

Blocks

A block is a sequence of zero or more statements enclosed by a matching set of curly braces ('{ }') and it acts as a single syntactic statement.

You can create a block from the set of two statements in the preceding example by enclosing the statements in matching curly braces, as shown in the following code:
{
 int var1 = 5;
 System.Console.WriteLine("The value of var1 is {0}", var1);
} 

Some important things to know about blocks are the following:
  • You can use a block whenever the syntax requires a statement but the action you need requires more than one simple statement.
  • Certain program constructs require blocks. In these constructs, you cannot substitute a simple statement for the block.
  • Although a simple statement is terminated by a semicolon, a block is not followed by a semicolon. (Actually, the compiler will allow it, because it’s parsed as an empty statement—but it’s not good style.)

Comments

When your code is getting bigger and more people work with your code, you need some inline documentation. Programmers insert comments into their code to explain and document it.

There are several type of comment available in C#:
Type Start End Description
Single-line // The text from the beginning marker to the end of the current line is ignored by the compiler.
Delimited /* */ The text between the start and end markers is ignored by the compiler.
Documentation /// Comments of this type contain XML text that is meant to be used by a tool to produce program documentation.

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
}