In this post we’ll learn about C# | Classes and Constructors. Below we are going to talk about:

  1. Adding New Elements in Solution Explorer
  2. Defining Classes and How to Use Them
  3. Constructors
  4. Constructor Overloading
  5. Partial Classes

Adding New Elements in Solution Explorer

Even though we can create new classes inside the Program.cs file, it is much better to create a new class in a separate file. To do that, we need to right-click on our project name, choose Add and then New Item (Ctrl+Shift+A). Post this we need to choose a class file and add a name:

31 CodeConfig csharp classes constructors - https://codeconfig.in

41 CodeConfig csharp classes constructors - https://codeconfig.in

Defining Classes and How to Use Them

In C#, to define a class, we need to use the class keyword. The class consists of members. All the class members are defined in the class body between two curly braces:

public class Student
{
    private string _name;
    private string _lastName;

    public string GetFullName()
    {
        return _name + ' ' + _lastName;
    }
 }

We see that the body contains two private fields (variables in the class body are called fields) _name and _lastName and one public method GetFullName (if you are not familiar with the access modifiers: private, public, etc. you can read more about them in our module 1 about C# basics).

Now the student object can access the members from the student class. For now, we have only one method inside the student class and we can call it with the student.GetFullName() syntax. This will return an empty string, but we are going to fix that as soon as we introduce constructors.

It is so important not to confuse the terms class and object. The class is a type definition but an object is an instance of that type. We can have several object instances of the same class:

class Program
{
    static void Main(string[] args)
    {
        Student student = new Student();
    }
}

Student student = new Student();
Student student1 = new Student();
Student student2 = new Student();
Student student3 = new Student();

Constructors

When you create an object using the new keyword, the CLR (common language runtime) uses the class definition to call the constructor method to create the object. A constructor is a special method that has the same name as the class in which it is defined, does not return a value (not even void), and can take parameters. It runs automatically when you create an instance of the class. Each time you instantiate a class using the new keyword, you call the constructor for that class.

Check more on Constructors:

All classes require a constructor. If you do not write it, the compiler will automatically generate it. This type of constructor is called a default constructor. The default constructor sets all data in the class to its default value (or the assigned value if not assigned). In this example, the _name and _lastName fields have leading null values. This is because it is the default value for reference types.

We can write our own default constructor as well:

public class Student
{
    private string _name;
    private string _lastName;

    public Student()
    {
        _name = string.Empty;
        _lastName = string.Empty;
    }

    public string GetFullName()
    {
        return _name + ' ' + _lastName;
    }
}

Constructor Overloading

Our classes are not limited to just one constructor method. You can create more of these in one class:

public class Student
{
    private string _name;
    private string _lastName;

    public Student()
    {
        _name = string.Empty;
        _lastName = string.Empty;
    }

    public Student(string name, string lastName)
    {
        _name = name;
        _lastName = lastName;
    }

    public string GetFullName()
    {
        return _name + ' ' + _lastName;
    }
}

Now we have two options to instantiate our class,

First one with the default values and second one is overloaded one, which gives us the ability to set the values of our fields:

class Program
{
    static void Main(string[] args)
    {
        Student student = new Student(); //default constructor

        Student student1 = new Student("John", "Doe");//overloaded constructor
        Console.WriteLine(student1.GetFullName());
    }
}

There is one important thing to have in mind. If we create our own constructor for a class, the compiler won’t create a default one for us. So, if we want to have a default one and the overloaded one, we must create both of them.

Partial Classes

In a real project, this could be a very large class with many, many lines of code. This type of class can be less readable and more difficult to maintain. To avoid this, you can use partial classes. Partial classes also have the added benefit of allowing multiple developers to work on the same class at the same time. Additionally, you can also create partial methods within these classes. In nutshell a partial class is nothing more than a part of a single class. To define partial classes, we need to use the partial keyword in each file:

partial class Student
{
    private string _name;
    private string _lastName;

    public Student()
    {
        _name = string.Empty;
        _lastName = string.Empty;
    }
}

partial class Student
{
    public Student(string name, string lastName)
    {
        _name = name;
        _lastName = lastName;
    }

    public string GetFullName()
    {
        return _name + ' ' + _lastName;
 

I hope you enjoyed reading this post. For more information you can check out Microsoft learn.

For more information you can visit our C# Section for more such posts.

Explore More

Working With File Class In C#

In C#, ‘File’ class provides many static methods for moving, copying, and deleting files. These Static methods involve moving a file and copying and deleting a file also. Let’s check

How to Read a Text File in C#

In C# ‘File’ class provides static methods to read given text file data. The File.ReadAllText() method opens a text file, reads all the text present in the file into a

C# | How to use Interface references

In C#, you’re permitted to make a reference variable of an interface type or in other words, you’re permitted to form an interface reference variable. Such kind of variable can