SYNTAX
# 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.C
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
SampleClass sampleObject = new SampleClass(); // constructing a new object
sampleObject.SampleInstanceMethod(); // calling an instance method
SampleClass.SampleStaticMethod(); // calling a static method
// 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
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, the protected statements of a try block, and the code within a corresponding catch block.
private void MyMethod()
{
// This block of code is the body of "MyMethod()"
CallSomeOtherMethod();
try
{
// Here is a code block protected by the "try" statement.
int variableWithLimitedScope;
// "variableWithLimitedScope" is accessible in this code block.
}
catch(Exception)
{
// Here is yet another code block.
// "variableWithLimitedScope" is not accessible here.
}
// "variableWithLimitedScope" is not accessible here, either.
CallYetAnotherMethod();
// Here ends the code block for the body of "MyMethod()".
}
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.
//**************************
10 | C# Programming
Syntax
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;
The following code will generate a compiler error (unless a custom class or variable named console has a method named writeline()):
// Compiler error!
console.writeline("Hello");
The following corrected code compiles as expected because it uses the correct case:
Console.WriteLine("Hello");
0 comments:
Post a Comment