Under what conditions am I supposed to make the :base()
and :this()
constructor calls following my constructor's parentheses (or even in other places in the code). When are these calls good practices and when are they mandatory?
相关问题
- how to define constructor for Python's new Nam
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- Keeping track of variable instances
Use
base
when there is inheritance, and a parent class already provides the functionality that you're trying to achieve.Use
this
when you want to reference the current entity (or self), use it in the constructor's header/signature when you don't want to duplicate functionality that is already defined in another constructor.Basically, using base and this in a constructor's header is to keep your code DRY, making it more maintainable and less verbose
Here's an absolutely meaningless example, but I think it illustrates the idea of showing how the two can be used.
Usage:
You use :base() when you want the constructor of the base class to be automatically called as first instruction of your constructor. :this() it's similar, but it call another constructor on the same class.
In base:() and this(): you can pass as parameters constant values , or expression based on parameters of you constructor.
It's mandatory to call the base constructor when the base class has no default constructor (one that takes no parameters). I don't know of a case in which :this() is mandatory.
: base(...)
If you omit the call to a base constructor it will call the default base constructor automatically.
It is mandatory to call a base constructor explicitly if there is no default constructor.
Even if there is a default constructor you may still wish to call a different constructor than the default constructor. In this case you may still wish to use
base(foo, bar)
to call a different constructor than the base constructor.I do not consider it to be a bad practice to omit
base()
when you want to call to the base class default constructor, although if you like to be explicit I see no harm in including it. It is a matter of taste.: this(...)
This syntax allows you to call one constructor with a different signature from another within the same class. It is never mandatory to do this, but can sometimes be useful.
An example of when it can be useful is for reusing common code in the constructors. For example in C# 3.5 or before you may want to simulate optional parameters on a constructor:
With C# 4.0 optional parameters are now available which reduces the need for this approach.
An alternative way to reuse code in constructors is to factor it out into a static function which is called from each constructor that wishes to use it.
Look for "constructor chaining in C#". Basically, it looks like this:
It helps to remove code duplication in constructors - split them into basic and specific parts.
First off, when they're mandatory.
When a class
Derived
is derived from a classBase
, andBase
does not have a default (parameterless) constructor,Derived
must callbase()
explicitly with parameters.When is it good practice? Whenever you want to call a different constructor.
Suppose you add, in my previous example, content to the constructors in Derived.
You notice the duplication here? It's simpler to call the this() constructor.