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
• To access base class method from derived class where in the same method is overridden in the derived class
• To access base class property from derived class where in both base class and derived class have the property with the same name

The example given below clearly demonstrates these purposes of using base keyword:

class ClassA {
int memberVar;
ClassA(int param) {
memberVar = param;
}
virtual void memberMethod( ){
Console.WriteLine(“Executing memberMethod of base class ClassA”);
}
}
class ClassB:ClassA {
int memberVar;
ClassB(int param):base(param) { } //Calling base class constructor
override void memberMethod( ){
Console.WriteLine(“In memberMethod of derived class ClassB”);
base.memberVar = 100;//accessing base class property
Console.WriteLine(“Value of memberVar of ClassA:{0}”,base.memberVar);
base.memberMethod( );//accessing base class method
}
}
class ClassC {
static void Main( ){
ClassB obj = new ClassB(20);
obj.memberMethod( );
}
}

Output of this example:

In memberMethod of derived class ClassB
Value of memberVar of ClassA:100
Executing memberMethod of base class ClassA

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
• You cannot use it inside a static method of derived class
• Only immediate base class in the inheritance tree can be accessed using base keyword. Consider the following example:
class class1 { }
class class2: class 1 { }
class class3: class2{ }
You can use base keyword in class3 to access only class2. You cannot access class1 inside class3 using base keyword even though it’s an indirect base class of class3.

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
• If you want to pass an instance of the class from itself as a parameter to a method then you can simply pass the keyword this to refer the current instance
• You can define indexers in your class using this keyword

Here is an example to illustrate usage of this keyword to accomplish above mentioned tasks:

class ClassA {
int param;
int[ ] memberArray = new int[2];
ClassA(int param) {
this.param = param;
}
void memberMethod(int param) {
ClassB.staticMethod(this);
}
public int this[int index] {
get { return memberArray[index]; }
set { memberArray[index] = value; }
}
}
class ClassB {
static staticMethod(ClassA obj) {
Console.WriteLine(“Value of param of ClassA is: {0}”, obj.param);
}
}
class ClassC{
static void Main( ){
ClassA objA = new ClassA(100);
objA.memberMethod( );
objA[0] = 10;
objA[1] = 11;
Console.WriteLine(“Fetching Value From Indexer:{0}, {1}”’, objA[0], objA[1]);
}
}

Output of this code will be:
Value of param of ClassA is: 100
Fetching Value From Indexer: 10, 11

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 ClassB’s 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 ClassC’s Main method using the instance objA.

_______________________________________________________________________

Add to My Yahoo!


FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

Visit .NET Programming Tutorial Homepage

______________________________________________________

Recommended Resource

|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)|

 


| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2017 - All Rights Reserved.