-3

Please, explain what " : this()" refer to.Cant I use "base" instead of it. When I move cursor on "this" it displays another Constructor . Or when I replace it with "base" it gives this error: "object doesnt contain a constructor that takes 1 argument".

class Employee
    {
        private string name;
        private string surname;
        private DateTime birthday;
        private double height;
        private double salary;
        public string Name { get { return name; } set { if (name != value) { name = value; } } }
        public string Surname { get { return surname; } set { if (surname != value) { surname = value; } } }
        public DateTime Birthday { get { return birthday; } set { if (birthday != value) { birthday = value; } } }
        public double Height { get { return height; } set { if (height != value) { height = value; } } }
        public double Salary { get { return salary; } set { if (salary != value) { salary = value; } } }
        public string Full { get { return name + " " + surname; } }
        public Employee() {
            name = "Mina";
            surname = "Babayeva";
            birthday = DateTime.Now.AddYears(-19);
            height=175;
            salary = 3500;
        }   

        public Employee(string name, string surname, DateTime birthday, double height, double salary) : this( name,  surname)
        {
            this.birthday = birthday;
            this.height = height;
            this.salary = salary;
        }
        public Employee(string name) : this()
        {
            this.name = name;
        }
        public Employee(string name, string surname) : this(name)
        {
            this.surname = surname;
        }


        public void Print()
        {
            Console.WriteLine("Name is {0}\nSurname is {1}\nHeight is {2}\nBirthday is {3}\nSalary is {4}", 
                this.Name, this.Surname, this.Height, this.Birthday.ToString("dd/MM/yyyy"), this.Salary);
        }


////////////////////////////////
 Employee a = new Employee();
            Employee b = new Employee("Adil", "Babayev");
             Employee c = new Employee("Zumrud","Babayeva",DateTime.Now.AddYears(-52),167,1000000);
            Employee e = new Employee("Hanna");


            a.Print();
            Console.WriteLine();
            b.Print();
            Console.WriteLine();
            c.Print();
            Console.WriteLine();
            e.Print();
            Console.WriteLine();

2 Answers2

0

In this context, this refers to the constructor for the current class. base refers to the constructor for the base/inherited class. So no, they are not interchangeable.

More info: MSDN Using Constructors

Abion47
  • 22,211
  • 4
  • 65
  • 88
0

Anytime you create a constructor for a class, you are actually creating an instance as a single copy of an object. The this keyword is that instance.

Example Class Apple : Orange {}

public Apple () :this() {} This. is a reference to the instance of Apple.

When calling base on a class constructor, you are calling the derived class constructor. Public Apple() : base() {}

would return the Orange Class Constructor, not Apples! Huge difference between the two!!

Please look into C# Constructor Chaining. MSDN has great examples and documentations in regards to your quest.
Good luck my friend

Kind Regards.

Ahhzeee
  • 123
  • 1
  • 12