< Prev
Next >

The first C# Program

To write your first simple C# program, please open the Notepad application(comes preinstalled in Windows Operating System) and type in your first C# program, as explained in this article.

You may even use an Integrated Development Environment(IDE) application like Microsoft Visual Studio and NetBeans to create your first C# program but for a beginner, we would recommend using Notepad, because you learn a lot when you work to find your own mistakes to fix them, as compared to an IDE finding them for you.

Important points about a class in C# -

C# naming conventions -

Note : Not following these naming conventions while naming an identifier won't raise any error i.e. C# compiler doesn't throw a compile error, if a class name starts with a lowercase letter, or if a multiple word method name is separated by b underscore (_), but, it is good to follow the naming conventions of defined by the language in which you are coding as it also improves the readability.

C# is a case-sensitive language

  • C# is a case-sensitive language, hence a class containing two variables with one named a and the other named A, are treated differently and methods named message() and Message() are treated as two separate methods.

  • Creating our first C# program

    We are creating a C# program which will be saved in a file named A.cs.

    Why .cs extension for C# program?

    Every C# program is saved with a .cs extension because cs stands for C Sharp.

    //C# A simple program
    using System;
    //Creating a class named A
    class A
    //Defining the method named Main() of class A
    public static void Main(String[] ar)
    	//Using the Console.WriteLine statement to print a message to the Console
    	Console.WriteLine("Hello from C#");


    Understanding your first C# program

    Let's see what we actually did in our first C# program by dividing it into a few segments -

    1. using System;

      • Every C# program that requires to perform input and output operations, must use an already defined System namespace by using keyword right at its beginning i.e. before any class is defined.

        Doing this, tells the compiler to look in the System namespace (namespace can be understood as a container for a set of classes), in order to use the methods or variables defined in it to perform input-output operations.

      • class A

        • Every C# program is contained within a class. Hence, we have created a class named, A, and it is defined within a pair of curly braces { }.

        • According to the C# convention, every class name in C# should start with a capital alphabet, hence, we have named our class A.

        • Any new class created in C# is automatically given the public access modifier, which makes this class accessible to all other classes in C#. Hence, our class A is accessible to all other C# classes.

          Note: Giving any other access modifier like private, protected, etc to a class in C# is illegal and is not allowed.

        • Unlike Java, in C#, the name of the program file with .cs extension need not to be matched with the name of public class defined in it.

        Note : Unlike Java, In C#, we can define any number of public classes in a single .cs program file.

      • public static void Main(String[] ar)

        • In our class, we have defined a Main() method, because every C# program begins its execution with a call to the Main() method of the class, which is also known as the entry point of the program to execute.

          The Main() method is always defined with a public access modifier, which makes it accessible from the outside of its class, to execute it.

        • Unlike Java, the Main() method in C# starts with a capital letter, by following its method naming conventions.

        • The Main() method is always defined with static keyword, which means that this method can be directly accessed/called, without having to instantiate or create an object of class in which it is defined, i.e. class A.

        • The return type of Main method is always void, which means that this method will not return any value after it finishes its execution.

        • The Main() method has one parameter named ar, which is an array of class type String, it means we can pass one or more objects of String class to the main method.

          If you don't understand much of what is explained so far in this program, don't worry, you will learn more about array, static keyword and String class in the upcoming articles.

      • Console.WriteLine("Hello from C#");

    This line simple outputs the string Hello from C# on the command prompt, where :


    How to compile a C# program?

    In order to compile our C# program, we first need to open the Developer Command Prompt for Visual Studio application, which was installed when we installed Microsoft Visual Studio.

    Next, on the window of Developer Command Prompt for Visual Studio, we need to call the C# compiler with a command - csc, followed by a space, followed by the the name of our file(with its extension) that contains the C# class.

    We have saved our C# program in a file A.cs in the E: Drive location. Hence, in order to compile our A.cs file from Developer Command Prompt for Visual Studio, first we need to reach the location where this C# file is saved and type in the command -

    E:\csc A.cs

    This will create a file, A.exe. This .exe file is a compiled form of our C# program, which is going to be required to execute our C# program.

    Note :

    The compile command csc stands for C Sharp Compiler.

    How to run a C# program?

    Hello from C#

    Please share this article -

    < Prev
    Next >
    < Download and Install Visual Studio
    C# Data Types >


    Please Subscribe

    Please subscribe to our social media channels for daily updates.

    Decodejava Facebook Page  Decodejava Twitter Page Decodejava Google+ Page



    Please check our latest addition