Optimizing constructor initializer?

NOTE. I work in .NET 3.5 here.

Let's say I have examples of base / child classes with the following constructors:

public Person(string name, int age, string job, bool isMale)
{  
    Name = name;
    Age = age;
    Job = job;
    IsMale = isMale;
}  

public CollegeStudent(string name) : this(name, 18) {}

public CollegeStudent(string name, int age) : this(name, age, "Student") {}

public CollegeStudent(string name, int age, string job) : this(name, age, job, true) {}

public CollegeStudent(string name, int age, string job, bool isMale) : base(name, age, job, isMale) {}

Is the compiler smart enough to make sure that the only thing the child constructors do is relate to each other and ultimately just call the base constructor? So, can it just change the initializers of the "this" constructor to invoke the base constructor directly at compile time?

Thus, this would significantly change all of this under the hood:

public CollegeStudent(string name) : base(name, 18, "Student", true) {}

public CollegeStudent(string name, int age) : base(name, age, "Student", true) {}

public CollegeStudent(string name, int age, string job) : base(name, age, job, true) {}

public CollegeStudent(string name, int age, string job, bool isMale) : base(name, age, job, isMale) {}

I would like to write my constructors in the same way as the first section, since it is convenient, but I can just call the base constructor directly for each constructor if I just take on useless utility data.

+3
5

# , Object. , , , . , , Reflector, .

.

Edit

(.Net 3.5):

namespace Person {
    public class Person {
        private String Name;
        private int Age;
        private String Job;
        private Boolean IsMale;

        public Person(string name, int age, string job, bool isMale) {
            Name = name;
            Age = age;
            Job = job;
            IsMale = isMale;
        }
    }

    public class CollegeStudent : Person {
        public CollegeStudent(string name) : this(name, 18) { }
        public CollegeStudent(string name, int age) : this(name, age, "Student") { }
        public CollegeStudent(string name, int age, string job) : this(name, age, job, true) { }
        public CollegeStudent(string name, int age, string job, bool isMale) : base(name, age, job, isMale) { }
    }
}

Reflector, , # , .

+6

- , JIT, #. , , # , CollegeStudent , .

SimpleCoder Jim, . , , .

, # 4 , , .NET 2.0 .NET 3.5, # 2 # 3 , .

+5

# 4.0, , , .

public Person(string name, int age = 18, string job = "Student", bool isMale = true)
{  
    Name = name;
    Age = age;
    Job = job;
    IsMale = isMale;
}

public CollegeStudent(string name) : base(name) {}

public CollegeStudent(string name, int age) : base(name, age) {}

public CollegeStudent(string name, int age, string job) : base(name, age, job) {}

public CollegeStudent(string name, int age, string job, bool isMale) 
                      : base(name, age, job, isMale) {}
+3

, " " , . ? , , , , , . , . ?

+2

:

CollegeStudent student = new CollegeStudent
{
  Name = "Bob",
  Age = 21,
  Job = "Spaceman",
  IsMale = true,
};

, , , Visual Studio.

+1

Source: https://habr.com/ru/post/1793271/


All Articles