Exclusively for you!! Trial version of Google Apps, Apply Coupon-CNHPGX6T6QRWGV!!

base and this keyword in C#

Csharp
Csharp

base and this keyword in C#

Welcome to CodeSpread!

I have seen programmers getting confused with keywords like ‘base’ and ‘this’. Although self-explanatory, still requires basic knowledge of scope to dive in deep.

Let’s take them one by one.

base keyword:

So by name, we can easily deduce that it is related to inheritance and mainly revolves around base and derived class. Let’s see an example to understand more.

public class SiteName
{

public  virtual void SiteInfo()
{
Console.WriteLine("Name: CodeSpread");
}
}
class Domain: SiteName
{

public override string SiteInfo()
{
base.SiteInfo()
Console.WriteLine("domain: www.codespread.com");
}
}

I know that’s a not a so good example but still eligible. In the derived class, we were able to call the parent method by using ‘base’ keyword so we can define ‘base’ keyword as, freedom to access (method, property etc.) of the base class from within a derived class.

Note:

  • Keep your eyes open for override functionality.
  • It is an error to use the base keyword from within a static method.

this keyword:

‘this’ keyword is more or so related to the current instance of the class.

So why we need it? let’s say in simple words ‘ to avoid naming conflicts’.

Note: Static constructors and member methods do not have a ‘this’ keyword.

There are few common uses:

  1. Names ambiguity.
  2. Constructor to constructor call.
  3. Indexer

Names ambiguity.

class Employee
{
string EmployeeName;
public Employee(string EmployeeName)
{
this.EmployeeName=EmployeeName;//this will refer to current instance else EmployeeName will refer itself
}

Constructor to constructor call.

class Contractor
{
public Contractor():Employee("CodeSpread") //calls base constructor: Employee(string EmployeeName)
{
}
}

Indexers

class ClassCollections

{

static void Main(string[] args)

{

NewCollection newCollection = new NewCollection();

newCollection [0] = “Hello” ;

newCollection [1]= “World”;

Console.WriteLine(newCollection [0]+newCollection [1]);

}

}

Please share your views or feedback to admin@codespread.com

Author: hershey

A passion for knowledge drives me to do programming, A passion for programming drives me to create something different, A passion for creation drives me to spread the knowledge.

Share This Post On

0 Comments

  1. nice superb explaination

    Post a Reply
  2. thanks for the tutorial
    I am having a problem.
    The feature “Create SQL server database” isn’t available , I can’t select it.
    Can you help, please ?

    Post a Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

More from CodeSpread:

  • Indexers,A Short Note.Indexers,A Short Note.What is an indexer? Indexers can be easily remembered as arrays. These arrays are special as they allow the class to be treated as arrays and access the data stored within the object. For example:...
  • Why Multiple Inheritance is discouraged?Why Multiple Inheritance is discouraged?Hi Guys, This is Sathish Again,Today we will discuss why most of the higher languages eliminates Multiple Inheritance? Most of you guys already know that multiple inheritance is not available for...
  • C#: Anonymous Type is not anonymous!C#: Anonymous Type is not anonymous!Why Anonymous Type? Anonymous types provide you the ease of having set of properties (Read only) into a single object without knowing the type. It’s the job of compiler to generate the type inform...
  • C#: Singleton Design PatternC#: Singleton Design PatternWe know the existence of a design pattern is dependent on a recurring problem. Once the problem is identified, then a well established solution is proposed theoretically and different implementatio...
  • Useful ‘ref’ and ‘out’ parametersUseful ‘ref’ and ‘out’ parameters It took me some time to realize the real potential of ref and out keywords, till the time I experience a condition which could only be simplified by implementing ref and out keyword. Lets st...
  • C#: My experience with const keywordC#: My experience with const keywordThere are numerous post on internet about 'what is const keyword in .net' or 'what is the difference between const keyword and read only' and I must say many might have already mugged it up like me...
  • Note: GZipStreamNote: GZipStreamA short note from my experience: From the name itself you can at least make a guess about the functionality of the GZipStream class(present under: System.IO.Compression). Yes..your guess is abso...
  • Create a browser in C# WinFormsCreate a browser in C# WinFormsHi Guys, This is Sathish, one of my friend was finding difficult in creating his own web browser, so I thought lets create a simple web browser in win forms of c# and share it with you. We are onl...