What is a Class in C#?

What is a Class in C# ?
What is a Class in C# ?

C# Class is another very common and important concept like namespace about which you must be aware of.  As you know, if we want to store some value for a number, string etc, we make use of built in data types like int, string etc.  Built in type are good in case you want to store some simple data.  But if you want to store some custom data, which consists of first name, last name, fax, phone number, location etc, you have to make use of a Class to create custom complex types.  These custom complex types comes very handy in case of custom data storage.  The Classes contains fields, properties, and methods.  Fields and properties represent the state of the Class and methods represent the behavior of the Class.

What is a Constructor and Constructor Overloading in C#?

Each and every Class has its own default Constructor, which is auto generated by the class behind the scenes.  Constructors in C# Class have same name as of class, which can or cannot take any parameter and does not return any value.  Constructor are basically used to initialize fields and properties because whenever you create any object of the class, the Constructor is called automatically depends upon which Constructor your are using.   The default Constructor does not take any parameter.  You can create your own custom Constructors based on the number of parameters and data type of those parameters.  This process is also known as Constructor Overloading.  Whenever you create your own custom Constructor for the Class, the default Constructor is overridden.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Hello
{
    class Student
    {
        string _studentname;

        public Student(string name)
        {
            this._studentname = name;
        }

        public void PrintName()
        {
            Console.WriteLine(_studentname);
        }

    }


    class Program
    {
        static void Main(string[] args)
        {
            Student S = new Student("Albert Pinto");
            S.PrintName();
        }
    }
}

 Output:

Albert Pinto
Press any key to continue . . .

What is Constructor Chaining in C#?

This is a very interesting concept and pretty much time saver if you have multiple Constructors in the class.  This can be explained better with an example, which is given below.  Suppose in your class you are having 2 Constructors, one which does not take any parameter and another which does take a parameter.  The second Constructor sets the value of _studentname field and if you call the PrintName method, you can see the name on the console window.  But problem arises when you create an object, which is using first Constructor and call the print function.  Now, it is obvious that our Constructor does not set any _studentname value and expecting any output would be useless.  In this case, we make use of Constructor Chaining, where we make use of this keyword to call second Constructor by providing default value and it will solve our problem.  Also a point to keep in mind, we make use of this keyword to represent instance of the class.  So instead of directly assigning value to _studentname variable, we can write it in this way (this._studentname).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Hello
{
    class Student
    {
        string _studentname;

        public Student():this("No Name")
        {
            
        }


        public Student(string name)
        {
            this._studentname = name;
        }

        public void PrintName()
        {
            Console.WriteLine(_studentname);
        }

    }


    class Program
    {
        static void Main(string[] args)
        {
            Student S = new Student();
            S.PrintName();
        }
    }
}

 Output:

No Name
Press any key to continue . . .

What is a Destructor in C#?

Like constructor, we can have a Destructor in the class.  Name of Destructor is always same as Class name.  Destructor contains tilde sign in front of them.  It does not take any parameter nor return any value.  Desctructor is basically used to clean up any resources, which a Class was holding on during its lifetime.  We don’t need to call them because they are called by Garbage Collector automatically.  In dotnet, you don’t need to work on memory management because Garbage Collector will do all the job for you and clean up all the objects from memory, which are no longer in use.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Hello
{
    class Student
    {
        string _studentname;

        public Student(string name)
        {
            this._studentname = name;
        }

        public void PrintName()
        {
            Console.WriteLine(_studentname);
        }

        //Destructor
        ~Student()
        {
        }

    }


    class Program
    {
        static void Main(string[] args)
        {
            Student S = new Student("Albert Pinto");
            S.PrintName();
        }
    }
}