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:
access base class constructor from derived class constructor
The example given below clearly demonstrates these purposes of using base keyword:
Output of this example:
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:
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:
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:
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.
to our mailing list and receive new articles Note
: We never rent, trade, or sell my email lists to Visit
.NET Programming Tutorial Homepage ______________________________________________________
Visit .NET Programming Tutorial Homepage
|Designing applications in .Net using Service Oriented Architecture | How to Create and Use Anonymous Delegates in C# (C Sharp) |How to Define Custom Attributes in C# (C Sharp)| How To Handle Errors While Developing ASP.NET Applications | How to Use Indexer in Classes and Interfaces of C# (C Sharp)| Illustration of Access Keywords (base, this) with Examples in C# (C Sharp) | Illustration of Null Coalesce Operator (??) in C# (C Sharp) |Indexers Vs Properties in C# (C Sharp) | More about Reserved Attributes of C# (C Sharp)| Overview of Unified Type System of C# (C Sharp) | Purpose of Delegates in C# (C Sharp) |How does Simple Object Access Protocol (SOAP) help in Internet Communication |How to Create and Use Anonymous Delegates in C# (C Sharp)|