# C# Constructor

In C#, a constructor is similar to a method that is invoked when an object of the class is created.

However, unlike methods, a constructor:

* has the same name as that of the class
* does not have any return type

### Create a C# constructor

Here's how we create a constructor in C#

```csharp
class Car {
  
  // constructor
  Car() {
    //code
  }

}
```

Here, `Car()` is a constructor. It has the same name as its class.

### Call a Constructor

Once we create a constructor, we can call it using the `new` keyword. For example,

```csharp
new Car();
```

In C#, a constructor is called when we try to create an object of a class. For example,

```csharp
Car car1 = new Car();
```

Here, we are calling the `Car()` constructor to create an object `car1`.

### Types of Constructors

There are the following types of constructors:

* Parameterless Constructor
* Parameterized Constructor
* Default Constructor

### 1.Parameterless Constructor

When we create a constructor without parameters, it is known as a **parameterless constructor**. For example,

```csharp
using System;
class Car
{
    // parameterless constructor
    public Car()
    {
        Console.WriteLine("Car Constructor");
    }
}

class Program
{
    static void Main()
    {

        Car c = new Car();
        Console.ReadLine();

    }
}
```

### 2.Parameterized Constructor

In C#, a constructor can also accept parameters. It is called a **parameterized constructor**. For example,

```csharp
using System;
class Car
{

    string brand;
    int price;

    // parameterized constructor
    public Car(string theBrand, int thePrice)
    {

        brand = theBrand;
        price = thePrice;
    }
}

class Program
{
    static void Main()
    {

        Car c = new Car("VW",100);
        Console.ReadLine();

    }
}
```

In the above example, we have created a constructor named `Car()`. The constructor takes two parameters: `theBrand` and `thePrice`.

### 3.Default Constructor

If we have not defined a constructor in our class, then the C# will automatically create a default constructor with an empty code and no parameters. For example,

```csharp
using System;
class Car
{
    string brand;
    int price; 
}

class Program
{
    static void Main()
    {

        Car c = new Car();
        Console.ReadLine();

    }
}
```

In the above example, we have not created any constructor in the `Car`class. However, while creating an object, we are calling the constructor.

`Car c = new Car();`

Here, C# automatically creates a default constructor. The default constructor initializes any uninitialized variable with the default value.

{% hint style="info" %}
Note: In the default constructor, all the numeric fields are initialized to 0, whereas string and object are initialized as null.
{% endhint %}

### C# Constructor Overloading

In C#, we can create two or more constructor in a class. It is known as constructor overloading. For example,

```csharp
using System;
class Car
{
    string brand;
    int price;

    // constructor with no parameter
   public Car()
    {
        Console.WriteLine("Car constructor");
    }

    // constructor with one parameter
    public Car(string brand)
    {
        Console.WriteLine("Car constructor with one parameter");
        Console.WriteLine("Brand: " + brand);
    }
}

class Program
{
    static void Main()
    {
        //call constructor with no parameter
        Car car1 = new Car();
        Console.WriteLine();

        //call constructor with parameter
        Car car2 = new Car("Bugatti");

        Console.ReadLine();
    }
}
```

In the above example, we have overloaded the `Car` constructor:

* one constructor has one parameter
* another has two parameter

  Based on the number of the argument passed during the constructor call, the corresponding constructor is called.

Here,

* Object `car1` - calls constructor with one parameter
* Object `car2` - calls constructor with two parameters


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://raviram.gitbook.io/c-programing/oop-part-1/c-constructor.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
