Constructor

Constructors

Constructor is a method with the same name as the class. This method is executed during the initialisation of the class. It's possible to have multiple constructor's. Depending on the passed variables the right constructor is executed

    public class Car
    {
        // private
        private string CarName;

        // ctors
        public Car(){}

        public Car(string carname)
        {
            CarName = carname
        }

        // methods
        public string GetCarName()
        {
            return CarName;
        }
    }

    public class MyProgram
    {
        public void Cars()
        {
            // create car
            var car = new Car();
            var carName = car.GetCarName() // null
            // create new car
            var carTwo = new Car("mercedes");
            var carTwoCarName = carTwo.GetCarName(); // obvious it's mercedes
        }
    }

This constructor

It's possible to have constructor invocation to call another constructor. this can be useful in cases of object wich can be initialized with several or different parameters. 

 public class Car
    {
        // private
        private string CarName;

        // ctors
        public Car() : this("niet opgegeven")
        {

        }

        public Car(string carname)
        {
            CarName = carname
        }

        // methods
        public string GetCarName()
        {
            return CarName;
        }
    }

    public class MyProgram
    {
        public void Cars()
        {
            // create car
            var car = new Car();
            var carName = car.GetCarName() // niet opgegeven
            // create new car
            var carTwo = new Car("mercedes");
            var carTwoCarName = carTwo.GetCarName(); // obvious it's mercedes
        }
    }

Base constructor

It's possible to call the base constructor during invocation of the constructor, with this you can pass parameters and initialize some base things etc

    public class CarBase
    {
        // properties
        public string CarName { get; set; }

        // Ctor
        public CarBase(string carName = "")
        {
            CarName = carName;
        }
    }

    public class Car: CarBase
    {
        // ctors
        public Car() : base("ferari")
        { }

        public Car(string carname)
        {
            CarName = carname
        }

        // methods
        public string GetCarName()
        {
            return CarName;
        }
    }

    public class MyProgram
    {
        public void Cars()
        {
            // create car
            var car = new Car();
            var carName = car.GetCarName(); // obvious it's ferari
            // create new car
            var carTwo = new Car("mercedes");
            var carTwoCarName = carTwo.GetCarName(); // obvious it's mercedes
        }
    }

{{opmerking.Naam}}:

{{opmerking.OpmerkingText}}

            

Saving your comment....

Naam is verplicht!
Email is verplicht!
Opmerking is verplicht!