Base () and this () are the best methods

Under what conditions should I create constructor calls :base() and :this() , following the constructor brackets (or even elsewhere in the code). When are these challenges good practice and when are they mandatory?

+61
inheritance instantiation constructor c # oop
Sep 26 '10 at 11:15
source share
5 answers

: base(...)

If you omit the call to the base constructor, it will automatically call the default base constructor.

It is mandatory to call the base constructor explicitly if there is no default constructor.

Even if there is a default constructor, you can still call a different constructor than the default constructor. In this case, you can still use base(foo, bar) to call a different constructor than the base constructor.

I do not consider it a bad practice to omit base() when you want to call the default constructor of the base class, although if you like to be explicit, I see no harm in including it. This is a matter of taste.

: this(...)

This syntax allows you to call one constructor with a different signature from another within the same class. This is never necessary, but can sometimes be useful.

An example of when this might be useful is the reuse of common code in constructors. For example, in C # 3.5 or before you want to model optional parameters for the constructor:

 Foo(int x, int y) { this.x = x; this.y = y; } Foo(int x) : this(x, 10) {} // y defaults to 10 

C # 4.0 advanced options are now available that reduce the need for this approach.

An alternative way to reuse code in constructors is to include it in a static function that is called from every constructor that wants to use it.

+83
Sep 26 '10 at 11:20
source share

First of all, when they are mandatory.

When the Derived class is derived from the Base class, and Base does not have a default constructor (without parameters), Derived must explicitly call base() with the parameters.

 public class Base { public Base(int i) { } } public class Derived : Base { // public Derived() { } wouldn't work - what should be given for i? public Derived() : base(7) { } public Derived(int i) : base(i) { } } 



When is this good practice? Whenever you want to call another constructor.

Suppose you added content to constructors in Derived in my previous example.

 public class Derived : Base { // public Derived() { } wouldn't work - what should be given for i? public Derived() : base(7) { Console.WriteLine("The value is " + 7); } public Derived(int i) : base(i) { Console.WriteLine("The value is " + i); } } 

Did you notice duplication here? It's easier to name this constructor ().

 public class Derived : Base { // public Derived() { } wouldn't work - what should be given for i? public Derived() : this(7) { } public Derived(int i) : base(i) { Console.WriteLine("The value is " + i); } } 
+27
Sep 26 '10 at 11:22
source share

Use base when there is inheritance, and the parent class already provides the functions that you are trying to achieve.

Use this if you want to refer to the current object (or yourself), use it in the header / signature of the constructor when you do not want to duplicate the functionality that is already defined in another constructor.

In principle, using the database, and this is in the header of the constructor, you need to save the DRY code, making it more convenient and less verbose

Here is an absolutely pointless example, but I think this illustrates the idea of ​​showing how these two can be used.

 class Person { public Person(string name) { Debug.WriteLine("My name is " + name); } } class Employee : Person { public Employee(string name, string job) : base(name) { Debug.WriteLine("I " + job + " for money."); } public Employee() : this("Jeff", "write code") { Debug.WriteLine("I like cake."); } } 

Using:

 var foo = new Person("ANaimi"); // output: // My name is ANaimi var bar = new Employee("ANaimi", "cook food"); // output: // My name is ANaimi // I cook food for money. var baz = new Employee(); // output: // My name is Jeff // I write code for money. // I like cake. 
+21
Sep 26 '10 at 11:43 on
source share

You use: base () when you want the constructor of the base class to be automatically called as the first instruction of your constructor .: this () it is similar, but it calls another constructor in the same class.

In the database :() and this (): you can pass constant or expression values ​​as parameters based on the parameters of your constructor.

It is mandatory to call the base constructor when the base class does not have a default constructor (which does not accept parameters). I do not know a case in which: this () is required.

 public class ABaseClass { public ABaseClass(string s) {} } public class Foo : AChildClass { public AChildClass(string s) : base(s) {} //base mandatory public AChildClass() : base("default value") {} //base mandatory public AChildClass(string s,int i) : base(s+i) {} //base mandatory } public class AnotherBaseClass { public ABaseClass(string s) {} public ABaseClass():this("default value") {} //call constructor above } public class Foo : AnotherChildClass { public AnotherChildClass(string s) : base(s) {} //base optional } 
+4
Sep 26 '10 at 11:21
source share

Find the "Constructor Chain in C #". It basically looks like this:

 MyClass():base() //default constructor calling superclass constructor { } MyClass(int arg):this() //non-basic constructor calling base constructor { //extra initialization } 

This helps to remove duplication of code in the constructors - to break them into main and specific parts.

+3
Sep 26 '10 at 11:20
source share



All Articles