Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Class Constructors | Introduction to Object-Oriented Programming (OOP)
C# Beyond Basics
course content

Contenido del Curso

C# Beyond Basics

C# Beyond Basics

1. Additional Structures & File Handling
2. Structs & Enumerators
3. Introduction to Object-Oriented Programming (OOP)
4. OOP Essentials
5. OOP Principles

bookClass Constructors

A class constructor is the same as a struct constructor. It is a method which is executed when an object is created.

The syntax for creating a constructor is the following:

cs

index

copy
1234567
class className { // ... other class code public className() { // code } }

For-example:

cs

index

copy
1234567891011121314151617
using System; class Player { public Player() { Console.WriteLine("A new player is created"); } } public class HelloWorld { public static void Main(string[] args) { Player p1 = new Player(); Player p2 = new Player(); Player p3 = new Player(); } }

You can see from the output that the Console.WriteLine statement inside the Player constructor is executed every time a new object is created.

Following is an example code from the Struct Constructors chapter. The term static is explained in the comments as it is more relevant here however it is not necessary to completely understand it as this concept will be explained in detail in later sections.

cs

index

copy
1234567891011121314151617181920212223
using System; struct Program { public static int totalPlayers = 0; struct Player { public int id; public Player() { id = totalPlayers++; Console.WriteLine($"New Player Object Created With ID {id}"); } } static void Main(string[] args) { Player player1 = new Player(); Player player2 = new Player(); Player player3 = new Player(); } }

In order to convert the Player struct into a class, all we need to do is to change the term struct to class:

cs

index

copy
12345678910111213141516171819202122232425262728
using System; class Program { // Unlike normal fields, a 'static' field can hold data even if there's no object. // Static fields have the same value for all objects. // Changing the value of a static field from one object changes it for all objects. // In this case the static field is part of the 'Program' class which represents the program itself // Therefore this field is essentially a global variable public static int totalPlayers = 0; struct Player { public int id; public Player() { id = totalPlayers++; Console.WriteLine($"New Player Object Created With ID {id}"); } } static void Main(string[] args) { Player player1 = new Player(); Player player2 = new Player(); Player player3 = new Player(); } }

Similarly, we can have constructors with some arguments as well. Usually, this method is used to make the initialization of class fields easier:

cs

index

copy
12345678910111213141516171819202122232425262728
using System; class Point { public double x; public double y; public Point(double x, double y) { // 'this' keyword is also used in classes just like how its used in structs this.x = x; this.y = y; } public void showCoordinates() { Console.WriteLine($"({x}, {y})"); } } class Program { static void Main(string[] args) { Point p1 = new Point(50, 70); Point p2 = new Point(70, 90); p1.showCoordinates(); p2.showCoordinates(); } }

At this point, we have covered most of the basic concepts of the Classes, and judging from the syntax and behavior, Classes seem to be exactly the same as Structs, however, that is not actually the case. As stated in the first chapter of this section, Structs are a limited version of Classes that only provide a basic functionality on the other hand the features of Classes are much more complex and broad. In this section we have covered all the features that are similar between Classes and Structs. In the next two sections we will learn all the intricacies of classes and object-oriented programming.

1. When is a constructor method called?
2. What will be the name of a constructor method in a class called "Animal"?
3. Which keyword is used before a constructor name?
When is a constructor method called?

When is a constructor method called?

Selecciona la respuesta correcta

What will be the name of a constructor method in a class called "Animal"?

What will be the name of a constructor method in a class called "Animal"?

Selecciona la respuesta correcta

Which keyword is used before a constructor name?

Which keyword is used before a constructor name?

Selecciona la respuesta correcta

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 9
We're sorry to hear that something went wrong. What happened?
some-alt