Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Introduction to Constructors | Constructors and Overloading
C# OOP Class Construction Drills

bookIntroduction to Constructors

When you create an object from a class in C#, you need a way to set up its initial state. This is where constructors come in. A constructor is a special method in a class that runs automatically when you create a new object, allowing you to initialize fields or perform setup tasks. Constructors are essential because they ensure that your objects start life with valid, predictable values. Without constructors, you would have to manually assign every field after object creation, which is error-prone and inefficient. In C#, constructors share the same name as their class, and you don't specify a return type, not even void. Constructors are tightly tied to object creation; whenever you use the new keyword, a constructor is called.

Program.cs

Program.cs

copy
12345678910111213141516171819202122232425262728
using System; namespace ConsoleApp { public class Person { public string Name; public int Age; // Default constructor public Person() { Name = "Unknown"; Age = 0; } } public class Program { public static void Main(string[] args) { Person person = new Person(); Console.WriteLine("Name: " + person.Name); Console.WriteLine("Age: " + person.Age); } } }

In the Person class above, the default constructor is the method named Person with no parameters. When you create a new Person object using new Person(), this constructor runs automatically. Inside the constructor, the fields Name and Age are set to their default values: "Unknown" and 0. This ensures that every new Person object starts with these values unless you change them later in your code. The constructor is only called once, at the moment the object is created, and cannot be called directly like other methods.

Car.cs

Car.cs

copy
12345678910111213141516
namespace ConsoleApp { public class Car { public string Make; public string Model; // Parameterized constructor public Car(string make, string model) { Make = make; Model = model; } } }
Note
Definition

A constructor is a special method in a class that is called automatically when a new object is created. Its main role is to initialize the object's fields and set up its initial state.

You have seen how a default constructor works, but sometimes you want to provide specific values when creating an object. This is where a parameterized constructor comes in. In the Car class example, the constructor takes two parameters, make and model, and uses them to initialize the corresponding fields. When you create a new Car object, you can pass these values like new Car("Toyota", "Corolla"), and the object will be initialized with the values you provide.

The key difference between default and parameterized constructors is flexibility. A default constructor is useful when you want every object to start with the same values, or when you need to create objects without specifying details right away. A parameterized constructor is better when you want to set up objects with unique values at the moment of creation. You can define both types in the same class, allowing you to choose the best way to create each object.

1. What is the main purpose of a constructor in a C# class?

2. Which method is called automatically when an object is created?

3. Can a class have more than one constructor in C#?

question mark

What is the main purpose of a constructor in a C# class?

Select the correct answer

question mark

Which method is called automatically when an object is created?

Select the correct answer

question mark

Can a class have more than one constructor in C#?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 1

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

bookIntroduction to Constructors

Desliza para mostrar el menú

When you create an object from a class in C#, you need a way to set up its initial state. This is where constructors come in. A constructor is a special method in a class that runs automatically when you create a new object, allowing you to initialize fields or perform setup tasks. Constructors are essential because they ensure that your objects start life with valid, predictable values. Without constructors, you would have to manually assign every field after object creation, which is error-prone and inefficient. In C#, constructors share the same name as their class, and you don't specify a return type, not even void. Constructors are tightly tied to object creation; whenever you use the new keyword, a constructor is called.

Program.cs

Program.cs

copy
12345678910111213141516171819202122232425262728
using System; namespace ConsoleApp { public class Person { public string Name; public int Age; // Default constructor public Person() { Name = "Unknown"; Age = 0; } } public class Program { public static void Main(string[] args) { Person person = new Person(); Console.WriteLine("Name: " + person.Name); Console.WriteLine("Age: " + person.Age); } } }

In the Person class above, the default constructor is the method named Person with no parameters. When you create a new Person object using new Person(), this constructor runs automatically. Inside the constructor, the fields Name and Age are set to their default values: "Unknown" and 0. This ensures that every new Person object starts with these values unless you change them later in your code. The constructor is only called once, at the moment the object is created, and cannot be called directly like other methods.

Car.cs

Car.cs

copy
12345678910111213141516
namespace ConsoleApp { public class Car { public string Make; public string Model; // Parameterized constructor public Car(string make, string model) { Make = make; Model = model; } } }
Note
Definition

A constructor is a special method in a class that is called automatically when a new object is created. Its main role is to initialize the object's fields and set up its initial state.

You have seen how a default constructor works, but sometimes you want to provide specific values when creating an object. This is where a parameterized constructor comes in. In the Car class example, the constructor takes two parameters, make and model, and uses them to initialize the corresponding fields. When you create a new Car object, you can pass these values like new Car("Toyota", "Corolla"), and the object will be initialized with the values you provide.

The key difference between default and parameterized constructors is flexibility. A default constructor is useful when you want every object to start with the same values, or when you need to create objects without specifying details right away. A parameterized constructor is better when you want to set up objects with unique values at the moment of creation. You can define both types in the same class, allowing you to choose the best way to create each object.

1. What is the main purpose of a constructor in a C# class?

2. Which method is called automatically when an object is created?

3. Can a class have more than one constructor in C#?

question mark

What is the main purpose of a constructor in a C# class?

Select the correct answer

question mark

Which method is called automatically when an object is created?

Select the correct answer

question mark

Can a class have more than one constructor in C#?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 1
some-alt