C Sharp Programming/Syntax

From Wikibooks, the open-content textbooks collection

< C Sharp Programming
Jump to: navigation, search
C# Programming
Cover | Introduction | Basics | Classes | The .NET Framework | Advanced Topics | Index


C sharp musical note



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.

Contents

[edit] Statements

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);
    }
}

[edit] Statement blocks

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 value)
{  // This block of code is the body of "MyMethod()"
 
   // The 'value' integer parameter is accessible to everything in the method
 
   int methodLevelVariable; // This variable is accessible to everything in the method
 
   if (value == 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()".

[edit] Comments

Comments allow inline documentation of source code. The C# compiler ignores comments. Three 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
This comment is used to generate XML documentation. Each line of the comment begins with "///".
/// <summary> documentation here </summary>

This is the most recommended type. Avoid using butterfly style comments. For example:

//**************************
// Butterfly style documentation comments like this are not recommended.
//**************************

[edit] Case sensitivity

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

The variables myInteger and MyInteger 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");
Personal tools
Create a book
  • Add wiki page
  • Collections help