C Sharp/Statements

From Meshplex

Jump to: navigation, search
Image:Csharp_programming.gif

Main Home

Basics
C# Tutorial Home
C# - Introduction to Visual Studio IDE
Introduction to C#
C# - Overview
C# - Statements
C# - Data Types
C# - Variables
C# - Operators
C# - Flow Control
C# - Variables II
C# - Functions and Methods
C# - Classes and Objects I
C# - Enumerations
C# - Dates and Times
C# - Random Numbers

Advanced
C# - Inheritance
C# - Polymorphism
C# - Garbage Collection
C# - Operator Overloading
C# - Encapsulation
C# - Properties
C# - Indexers
C# - Exceptions
C# - GUI
C# - Delegates
C# - Events
C# - Components
C# - Multithreading
C# - Regular Expressions
C# - Graphics and Multimedia
C# - Files and Streams
C# - XML
C# - Database, SQL and ADO.NET
C# - ASP.NET Web Forms and Web Controls
C# - Web Services
C# - Network Programming
C# - Datastructures and Collections
C# - Enumerations and Iterators
C# - .NET Assemblies
C# - CLR
C# - Visual Studio Debugger
C# - Namespaces
C# - Generics
C# - MS Intermediate Language
C# - Deploying Windows Application

[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.