C# Statements Tutorial For Beginners With Examples

A statement is a complete program instruction.
  • All C# statements end with a semi-colon.
  • C# statements are case sensitive.
In this section let's try to understand the basic rules which should be followed to write C# statements.

Understand the C# Terminology

A list of common terminologies used in a standard C# program is listed below. This will help you to understand the C# program structure better.
ClassClass is a representation of a type. It specifies what the type can do. A type can be a person, tree, database table and so on.
StructureSame like a class but differs in the way they are created and stored in memory.
NamespaceCollection of classes.
MembersPart of a Class or Structure and represents their data and behavior.
Access ModifiersAll types (Classes and Structures) or Members have a accessibility level. This is defined by the Access Modifiers.
IdentifiersA name that is used to identify a class, structure, member or any user defined item.
KeywordsKeywords have special meaning to the C# compiler and are reserved by the system.
AssemblySmallest unit of deployment in a .NET application. It consists of Types and Resources. It could be a dll or exe.
Let's now take the example of the "Hello World" program that we wrote in the C# Editors chapter and try to understand it.

C# Hello World! code

    using System; 

    namespace HelloWorld
    { 
        class Program 
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Hello World!"); 
                Console.ReadLine(); 
            } 
        } 
    }
    
  • The first line of the above code says Using System;. Here System is the name of the namespace. If we want to use the classes defined inside System namespace then we have to use this line in the start of our code. We can have multiple using statements in a program.
  • The next line says namespace HelloWorld. To group the classes namespace is used here. Ideally similar type of classes should be part of a specific namespace.

    • A namespace should start with the namespace keyword.
    • A namespace, class or member should always start and end with curly braces {}.
  • A class should also start with the class keyword. In the above example the name of our class is Program and it is inside the namespace HelloWorld.
  • static void Main(string[] args) is the starting point of our program. To run the program properly the main method should have this exact signature.
    • Main is the name of the method.
    • static means the program can run without creating a instance of the Main method.
    • void means the Main method will not return anything.
    • string[] args is the parameter the Main method will accept. We can pass an array of strings to the main method and perform some operations on these strings. You will learn more on arrays in the later sections of this tutorial.
  • Console is a static class provided by C# and contains many important methods and properties to interact with the user's screen.
    • Console.WriteLine is used to write data to the console window.
    • Console.ReadLine is used to read user Input. Until the user provides some input, the console window will remain open. We have written this line of code to keep the window open so that output is visible to the user.