CSharp/C# Tutorial - C# Syntax






C# syntax is inspired by C and C++ syntax. And it has similar syntax with Java as well.

In this section, we will describe C#'s elements of syntax, using the following program:


using System; 

class Main { 
    static void Main() {
       int x = 2 * 3; 
       Console.WriteLine (x); 
    } 
} 




Identifiers and Keywords

Identifiers are names that programmers choose for their classes, methods, variables, and so on.

C# identifiers are case-sensitive. aMethod and AMethod are two different names.

By convention, parameters, local variables, and private fields should be in camel case, for example myVariable.

All other identifiers should be in Pascal case, for example MyMethod.

Keywords are names reserved by the compiler that you can't use as identifiers.

Here is the full list of C# keywords:


abstract     do        in            protected     true 
as           double    int           public        try 
base         else      interface     readonly      typeof 
bool         enum      internal      ref           uint 
break        event     is            return        ulong 
byte         explicit  lock          sbyte         unchecked 
case         extern    long          sealed        unsafe 
catch        false     namespace     short         ushort 
char         finally   new           sizeof        using 
checked      fixed     null          stackalloc    virtual 
class        float     object        static        void 
const        for       operator      string        volatile 
continue     foreach   out           struct        while 
decimal      goto      override      switch 
default      if        params        this 
delegate     implicit  private       throw 




Avoiding conflicts

To use a keyword as an identifier, qualify it with the @ prefix.

For instance:

class class {...} // Illegal 

class @class {...} // Legal 

We cannot use class as the name of a class directly, we have to add the @ before it.

The @ symbol is not part of the identifier itself. So @myVariable is the same as myVariable.

The @ prefix is useful when using libraries written in other .NET languages that have different keywords.

Contextual keywords

Some keywords are contextual, they can be used as identifiers-without an @ symbol.

These are:


add          ascending            async         dynamic 
equals       from                 in            into 
join         partial              remove        select 
where        yield                await         get 
let          set                  by            global 
on           value                descending    group 
orderby      var 

Comments

C# offers two different styles of source-code documentation: single-line comments and multiline comments.

A single-line comment begins with a double forward slash and continues until the end of the line.

For example:


int x = 3; // Comment about assigning 3 to x 

Single line comment is useful when adding shorter comment.

A multiline comment begins with /* and ends with */. For example:


int x = 3; /* This is a comment that 
spans two lines */ 

To add comment with multiple lines we should use the multiline comment.

Comments may embed XML documentation tags.