■
Note
It is a compiler error to use the
this
keyword within the implementation of a static member (explained
shortly). As you will see, static members operate on the class (not object) level, and therefore at the class level,
there is no current object (thus no
this
)!
Chaining Constructor Calls Using this
Another use of the this keyword is to design a class using a technique termed constructor chaining.
This design pattern is helpful when you have a class that defines multiple constructors. Given the
fact that constructors often validate the incoming arguments to enforce various business rules, it
can be quite common to find redundant validation logic within a class’s constructor set. Consider
the following updated Motorcycle:
class Motorcycle
{
public int driverIntensity;
public string driverName;
public Motorcycle() { }
Do'stlaringiz bilan baham: |