Tutorials Home

PHP Tutorials
CSharp Tutorials

  • CSharp Basics

  • CSharp Basics
    CSharp Tutorials, Concepts, Tips & Interview Questions

    CSharp Basics

    | More


    C# is pronounced as "C sharp". It is a programming language that enables programmers in quickly building solutions for the Microsoft .NET platform.
    It has a file name extension as "cs" which means csharp! As the first step, we will understand the basic structure of a C# program.
    Code:

    try.cs

    class MyFirstClass
    {
    static void Main()
    {

    }
    }

  • Here we start with the class MyFirstclass followed by open and close curly braces. A class is nothing but a collection - a collection of everything that the programming language contains. It is like a packet, which can hold anything. Everything in a C# program must be enclosed within a class.
  • Anything followed by '()' brackets is called a function. So, it is obvious that Main is a function.Main() is followed by the '{ }' curly braces. Note that the letter M in Main is capital.
  • Thus C# needs a function called Main, which is the first function that will be executed always. Failure to do so will result in an error.Main() is called as an entry point to any program.
    These rules have to be remembered. You have no choice.

    Now adding few more lines to the program code.
    Code:

    try.cs

    class MyFirstClass
    {
    static void Main()
    {
    System.Console.WriteLine("Hurray!!!");
    }
    }

    Output:
    Hurray!!!

    We are calling a function WriteLine but in fact as told before everything in C# will be inside a class. So, this WriteLine function must be in some class. The name of that class is Console.
    So, you might be thinking what the heck is System?
    Well, System is called a NameSpace. Dont worry,

    Namespace: It is nothing but it contains some groups of classes. Just like classes contains functions these namespaces contain classes.
    Namespaces are designed to help you organize your programs. They provide assistance in avoiding name clashes between two sets of code. Implementing Namespaces in your own code is a good habit because it is likely to save you from problems later when you want to reuse some of your code.
    For example, if you created a class named MyBills, you would need to put it in your own namespace to ensure that there wouldn't be any confusion when one of your friends also creates a class named MyBills in the same directory. Confusion arises when one of you calls MyBills class. To avoid that if you put it in a namespace of a particular name then you can call it Yourname.MyBills.functionName();

    Similarly, Microsoft has put all the functions that are related to Console Applications in Console class and in a namespace called System. Hence we call WriteLine() as System.Console.WriteLine(). It is called fully qualified name.

    using Directive:
    If you would like to call methods without typing their fully qualified name, you can implement the using directive.The first, using System, is the same using directive you will see in every program in this tutorial. It allows you to type the method names of members of the System namespace without typing the word System every time. In the example, Console is a class member of the System namespace with the method WriteLine(). Its fully qualified name is System.Console.WriteLine().
    Without the using directive, we would have to type System.Console.WriteLine() every time we wanted to call that method.
    Eg:
    Code:

    // Namespace Declaration is done with using directive
    using System;

    // Program start class
    class MyFirstClass
    {
    // Main begins program execution.
    public static void Main()
    {
    // Call namespace member
    Console.WriteLine("Hurray!!!I have made use of Namespaces.");
    }

    }

    Output:
    Hurray!!!I have made use of Namespace.


    using Alias Directive:Sometimes you may encounter a long namespace and wish to have it shorter. This could improve readability and still avoid name clashes with similarly named methods.So we create an alias(meaning alternative name) with the alias directive like following. If we want to make use of namespace with the name csharptutorials_from_studentschillout.com_website then we can give an alias name to it like below.
    using CStutorial = csharptutorials_from_studentschillout.com_website
    Now the expression CStutorial can be used anywhere, in the program, in place of csharptutorials_from_studentschillout.com_website.
    Eg:
    Code:

    // Namespace Declaration
    using System;

    // declaring alias name for our lengthy namespace name
    using CStutorial = csharptutorials_from_studentschillout.com_website;

    // Program start class
    class MyFirstClass
    {
    // Main is the entry point for the program execution.
    public static void Main()
    {
    // Calling a method which is supposed to be present in our imaginary namespace CStutorial

    CStutorial.MyBills();

    Console.WriteLine("Hurray!! I made use of Namespaces with the using and alias Directives.");
    }
    }

    Output:
    Hurray!! I made use of Namespaces with the using and alias Directives.



    So , Far we have covered class, namespace, using and alias Directives.
    In Future lessons we will know more about the programming.

    Share your views on the tutorial and i invite you to submit your view of understanding of the subject so that other readers will also be benefited by that.




  • | More