|
[edit] C# Blocks
Blocks of code occur between an open and closed curly braces. You are able to create blocks to follow a series of steps to satisfy a certain task or condition. Take a look at the proceeding code.
if (test == 0)
{
int s = 0;
Console.WriteLine(s);
}
Things to remember about blocks are:
- if a task requires you to use more than one statement use blocks
- Sometimes blocks are not avoidable like multiple statements in a task
- It is not good practice to end a curly brace with a semicolon.
You can end a curly brace with a semicolon and surprisingly the compiler will allow it. But, Microsoft may change this rule in the future so beware. Here is an example:
using System;
class Program
{
static void Main(string[] args)
{
int e = 8;
if (e == 0)
{
int s = 0;
Console.WriteLine(s);
};
}
}
Do you see the semicolon after the curly brace? Try running this code to see if it gives you an error.
|