ASP.NET Hosting

ASP.NET Tutorial: Constructor in C#

Constructor is a particular method provided by a class that is in charge of initializing the variables of that class.

A few words regarding constructor

  • The name of a constructor method is the same as the name of the class in which it is found.
  • Because the constructor never returns a value, we don’t need to define a return type for it.
  • There are two kinds of constructors: implicit constructors and explicit constructors.
  • If the programmer fails to define an implicit constructor, it is defined by the compiler. It is a constructor with no parameters.
  • The programmer defines an explicit constructor. It can have no parameters or parameters.
    In our main method, we call constructor by generating an object.

Types of Constructors

  1. Default or parameter less Constructor.
  2. Parameterized Constructor.
  3. Copy Constructor.
  4. Static Constructor.
  5. Private Constructor:

Default/parameter less Constructor

If a constructor method doesn’t take any parameters then we call that as default or parameter less.

  • It is automatically provided by the compiler if no constructor is defined.
public class MyClass
{
    // Default Constructor
    public MyClass()
    {
        // Initialization code
    }
}

Parameterized Constructor

If a constructor method takes any parameter then it is called a parameterized constructor. These constructors can be defined by the programmers only but never can be define by implicitly.

  • It allows you to initialize the object with specified values during creation.
public class Car
{
    // Parameterized Constructor
    public Car(string make, string model, int year)
    {
        Make = make;
        Model = model;
        Year = year;
    }
}

Copy Constructor

If we want to create multiple instances with the same values then we use these copy constructors, in a copy constructor the constructor takes the same class as a parameter to it.

  • C# doesn’t have a direct syntax for copy constructors, but you can achieve similar functionality using methods or other techniques.
class technicalscripter {

    // variables
    private string topic_name;
    private int article_no;

    // parameterized constructor
    public technicalscripter(string topic_name, int article_no)
    {
        this.topic_name = topic_name;
        this.article_no = article_no;
    }

    // copy constructor
    public technicalscripter(technicalscripter tech)
    {
        topic_name = tech.topic_name;
        article_no = tech.article_no;
    }

    // getting the topic name and
    // number of articles published
    public string Data
    {

        get
        {
            return "The name of topic is: " + topic_name +
                   " and number of published article is: " +
                                    article_no.ToString();
        }
    }
}

Static Constructor

If a constructor is explicitly declared by using static modifier, we call that as Static Constructor. All the constructor we have define till now are non-static. Static constructors are implicitly call no explicit call is required.

public class MyClass
{
    // Static Constructor
    static MyClass()
    {
        // Initialization code for static members
    }
}

Private Constructor

  • A constructor with private access modifier is called a private constructor.
  • It is often used in classes that contain only static members or as part of design patterns.
public class Singleton
{
    private static Singleton instance;

    // Private Constructor
    private Singleton()
    {
        // Initialization code
    }

    public static Singleton Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new Singleton();
            }
            return instance;
        }
    }
}

Thank you and Happy Learning.

ASP.NET Core 8 Hosting Recommendation

HostForLIFE.eu
HostForLIFE.eu is a popular recommendation that offers various hosting choices. Starting from shared hosting to dedicated servers, you will find options fit for beginners and popular websites. It offers various hosting choices if you want to scale up. Also, you get flexible billing plans where you can choose to purchase a subscription even for one or six months.