[edit] C# Statements
An easy way to remember what a C# statement is - is to remember that statements are always terminated by a semicolon. Examine the code below:
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("This is a statement");
Console.Read();
}
}
Any line that ends with a semicolon is a statement
Statement Blocks:
Statement blocks encloses multile lines of statements with curly braces. As discussed before it is allowed for curly braces to end with a semicolon but not recommended.
Single Statement:
In some cases you may only have one statement to run. If you have one statement to execute statement blocks are not required. Take a look at the following code:
Figure A
using System;
class Program
{
static void Main(string[] args)
{
int a = 1;
if(a == 1)
Console.WriteLine("This is a statement"); //No curly braces
}
}
Figure B
using System;
class Program
{
static void Main(string[] args)
{
int a = 1;
if (a == 1)
{
//This block uses curly braces
Console.WriteLine("This is a statement");
}
}
}
Both pieces of code above perform exactly the same function. The only difference is that Figure A uses no curly braces and Figure B uses curly braces.
|