• Startseite
  • Tutorials
  • Kontakt
  • Mein Account
Panjutorials
  • Startseite
  • Tutorials
  • Kontakt
  • Mein Account

Creating and using a Constructor

As you’ve probably already noticed, we need to learn how to build and use a constructor in CSharp to get started with classes.

What does the constructor do?

The constructor allows us to give certain properties to an object when we create it. In the concrete example from the last article, we had the properties, first name, name, age and eye color. If we now want to have all four for each object of the Person type, we need a constructor that can start with these variables.

You can imagine the constructor as a function that sets attributes for our object.

The best way to look at this is:

Example for creating and using a constructor in CSharp

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Panjutorials
{
    class Person
    {
        // attributes of the class Person
        int age;
        string firstname, lastname, eyeColor


        // The constructor has the same name of the class
        // er besitzt keinen Rückgabewert
        public Mensch(int myAge, string myFirstName, string myLastName, string myEyeColor)
        {
            age = myAge;
            firstname = myFirstName;
            lastname = myLastName;
            eyeColor = myEyeColor;
        }


        // This is an example of an introduction
        public void introduceOneSelf()
        {
            Console.WriteLine("My name is " + firstname + " " + lastname + " and I am " + age + " years old " +
                " and my eye color is " + eyeColor + ".");
            //Console.Read();

        }

    }
}

As you see the contructor has exactly the same name as the class, even the capitalization. I also commented out Console.Read() as I want to call it within the Program.cs class.

If we want to create an object of class Person, we can do that in our main class, which looks as following:

class Program
{
    static void Main(string[] args)
    {
        // declaring the two Objects of Person
        Person denis = new Person(28, "Denis", "Panjuta", "green");
        Person michi = new Person(35, "Michael", "Müller", "blue");
        
        // Calling the action of each of the two objects
        denis.introduceOneSelf();
        michi.introduceOneSelf();

        // output to console
        Console.Read();

    }
}

 

So you see, it is crucial that we created the constructor correctly and use the correct data types as parameters when creating an object of the class. In this way, we can now create arbitrary classes and objects and use their actions. This may be rather trivial in the example, but in a more complex program, e.g. a game, there are all kinds of classes. For example, Candy Crush, there every level is an instance of a class, every single candy is a subclass of the class sweets, etc. What subclasses are, will be covered in the chapter on inheritance.

NOTE: Classes can have multiple constructors. More about this in a later article.