Let's learn about classes and constructors in Object Oriented Programming using the C# programming language. We are going to create a simple program where we will learn how to declare a class and a constructor. I am also going to discuss parameterless and parameterized constructors and how we can use them to initialize an object.
What is a Class
A class is a model for creating objects. It defines the attributes, properties, and methods shared by all objects created from it. If two objects are instantiated from the same class, each object will share the same properties of the class, though they may have different property values.
Declaring a Class
Let’s start by creating a Student
class:
public class Student
{
}
A class declaration consists of:
- Access Modifier: This sets the class access level. The public keyword means we can access and instantiate this class from other classes. If we do not specify any access modifier in our declaration, it defaults to
internal
. - Class keyword: This is a reserved keyword in C#. We are using it here to declare the class.
- Identifier: This can be any name we give to the class. It must start with a letter or an underscore character (_). A naming convention in C# is using pascal casing for class names (e.g StudentLoan)
What is a Constructor
A constructor is a special method of a class that we can automatically call anytime we instantiate a class. Constructors set the initial values for object fields and can contain any code that executes at the time of object creation. If we do not provide a constructor in our class, the C# compiler creates a default constructor for us and it sets the fields to their default values.
Declaring a Constructor
Let’s create a constructor for the Student
class we declared earlier:
public class Student
{
public Student()
{
}
}
A constructor declaration consists of:
- Access modifier: Just like in our class declaration, the constructor also has an access modifier. If we do not specify any access modifier in our declaration, it defaults to
private
. This makes this class inaccessible from classes outside of it. - Identifier: The constructor identifier must have the same name as the class name. Unlike classes that we can give any valid name, specifying an identifier different from the class name will result in a compile-time error. This is because C# will treat the constructor like a class method that requires a return value in its declaration.
- Constructor parameters: Constructors can accept parameters or have no parameter.
The
Student
constructor we declared above accepts no parameter and is known as aparameterless
constructor.
public class Student
{
private readonly int _age;
private readonly string _name;
public Student()
{
}
public Student (string studentName, int studentAge)
{
_name = studentName;
_age = studentAge;
}
}
Here, we declare another Student
constructor that accepts parameters which we then use to set the fields’ initial values. This type of constructor is known as a parameterized
constructor. It allows us to pass in values for the fields during object creation.
Instantiating a class
Now we are going to instantiate the Student
class using its constructors:
public class Student
{
private readonly int _age;
private readonly string _name;
public Student()
{
}
public Student (string studentName, int studentAge)
{
_name = studentName;
_age = studentAge;
}
static void Main()
{
var student = new Student();
var johnson = new Student("Johnson", 12);
}
}
Classes are instantiated in C# using the new
operator. It is what invokes the class constructor.
We are passing no argument to the student
object and as a result, it calls our first constructor (parameterless) because it matches its argument list.
The constructor initializes the two fields to their default values. Here, _age
is set to zero, and _name
is set to null. To learn more about the default values of C# data types, this Microsoft guide can be useful.
To initialize the johnson
object, we are passing the same arguments as stated in the declaration of our parameterized constructor. The constructor accepts these values and assigns them to the fields. Here, _age
is set to 12, and _name
is set to Johnson
We can see here that we have declared two constructors in a single class each with a different number of parameters. This is allowed in C# and is known as Constructor overloading.
Conclusion
In this article, we have learned about classes and constructors in OOP using C# and how they can be declared. We studied two different types of constructors: parameterless and parameterized and how they can be used to initialize an object. Lastly, we saw how constructor overloading allows us to declare multiple constructors in a single class.
Top comments (0)