
Illustration of Access Keywords (base, this) with Examples in C# (C Sharp)C# supports different keywords like operator keywords, conversion keywords, access keywords, literal keywords, contextual keywords and many more. This article will focus on access keywords. C# provides two access keywords namely base and this.
If you want
to access member of a base class from derived class then you can do it
using base keyword. If you want to access an instance of the class inside
itself then you can use this keyword. Detailed illustration of both these
keywords is provided below. Access
Keyword base: When incorporating
inheritance if you want to explicitly access base class members inside
derived class then use base keyword. It is used inside derived class for
the following purposes: To
access base class constructor from derived class constructor The example
given below clearly demonstrates these purposes of using base keyword: class ClassA
{ Output of
this example: In memberMethod
of derived class ClassB In this example,
ClassA is the base class and ClassB is the derived class. Both ClassA
and ClassB have a property with the same name called memberVar. Base class
constructor is called from derived class constructor. And memberVar and
memberMethod( ) of base class is accessed inside memberMethod( ) of derived
class using the base keyword. There are
certain restrictions in using base keyword. They are listed below: You
can use it only inside the constructor or instance property accessor or
instance method of the derived class Access Keyword
this: You can access
a class member from outside the class by creating an instance of it. What
if you want to access the instance of a class from that class itself?
You can do it using this keyword. You can use this keyword in any instance
method of the class but not in any static method. You can perform the
following tasks using this keyword: If
the parameters passed to your instance method have the same name as that
of your class members, then you can differentiate those parameters from
class members using this keyword Here is an
example to illustrate usage of this keyword to accomplish above mentioned
tasks: class ClassA
{ Output of
this code will be: In this example,
parameter passed to the constructor of ClassA uses the same name as that
of the member variable termed param. Hence you use this.param to refer
the member variable. You pass an instance of ClassA from itself to ClassBs
staticMethod as ClassB.staticMethod(this). ClassB.staticMethod
is then used to print value of member variable param of ClassA. You also
have an indexer in ClassA to easily access the class member memberArray.
That indexer is defined as this[int index] and it is accessed in ClassCs
Main method using the instance objA.
_______________________________________________________________________
FREE
Subscription
Subscribe
to our mailing list and receive new articles Note
: We never rent, trade, or sell my email lists to Visit
.NET Programming Tutorial Homepage ______________________________________________________ |