C# Programming/Syntax

From Wikibooks, open books for an open world
Jump to: navigation, search

C# syntax looks quite similar to the syntax of Java because both inherit much of their syntax from C and C++. The object-oriented nature of C# requires the high-level structure of a C# program to be defined in terms of classes, whose detailed behaviors are defined by their statements.

Statements[edit]

The basic unit of execution in a C# program is the statement. A statement can declare a variable, define an expression, perform a simple action by calling a method, control the flow of execution of other statements, create an object, or assign a value to a variable, property, or field. Statements are usually terminated by a semicolon.

Statements can be grouped into comma-separated statement lists or brace-enclosed statement blocks.

Examples:

int sampleVariable;                           // declaring a variable
sampleVariable = 5;                           // assigning a value
Method();                                     // calling an instance method
SampleClass sampleObject = new SampleClass(); // creating a new instance of an object
sampleObject.ObjectMethod();                  // calling a member function of an object
 
// executing a "for" loop with an embedded "if" statement 
for(int i = 0; i < upperLimit; i++)
{
    if (SampleClass.SampleStaticMethodReturningBoolean(i))
    {
        sum += sampleObject.SampleMethodReturningInteger(i);
    }
}

Statement blocks[edit]

A series of statements surrounded by curly braces form a block of code. Among other purposes, code blocks serve to limit the scope of variables defined within them. Code blocks can be nested and often appear as the bodies of methods.

private void MyMethod(int integerValue)
{  // This block of code is the body of "MyMethod()"
 
   // The 'integerValue' integer parameter is accessible to everything in the method
 
   int methodLevelVariable; // This variable is accessible to everything in the method
 
   if (integerValue == 2)
   {
      // methodLevelVariable is still accessible here     
 
      int limitedVariable; // This variable is only accessible to code in the, if block
 
      DoSomeWork(limitedVariable);
   }
 
   // limitedVariable is no longer accessible here
 
}  // Here ends the code block for the body of "MyMethod()".

Comments[edit]

Comments allow inline documentation of source code. The C# compiler ignores comments. These styles of comments are allowed in C#:

Single-line comments
The // character sequence marks the following text as a single-line comment. Single-line comments, as one would expect, end at the first end-of-line following the // comment marker.
Multiple-line comments
Comments can span multiple lines by using the multiple-line comment style. Such comments start with /* and end with */. The text between those multi-line comment markers is the comment.
//This style of a comment is restricted to one line.
/* 
   This is another style of a comment.
   It allows multiple lines.
*/
XML Documentation-line comments
These comments are used to generate XML documentation. Single-line and multiple-line styles can be used. The single-line style, where each line of the comment begins with //, is more common than the multiple-line style delimited by /** and */.
// <summary> documentation here </summary>
// <remarks>
//     This uses single-line style XML Documentation comments.
// </remarks>
 
 
/** 
 * <summary> documentation here </summary>
 * <remarks>
 *     This uses multiple-line style XML Documentation comments.
 * </remarks>
 */

Case sensitivity[edit]

C# is case-sensitive, including its variable and method names.

The variables myInteger and MyInteger of type int below are distinct because C# is case-sensitive:

 int myInteger = 3;
 int MyInteger = 5;

For example, C# defines a class Console to handle most operations with the console window. Writing the following code would result in a compiler error unless an object named console had been previously defined.

 // Compiler error!
 console.writeline("Hello");

The following corrected code compiles as expected because it uses the correct case:

 Console.WriteLine("Hello");