24

I am interested to know the difference between this and base object in C#. What is the best practice when using them?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Abhishek
  • 957
  • 3
  • 20
  • 43

9 Answers9

42

this represents the current class instance while base the parent. Example of usage:

public class Parent
{
    public virtual void Foo()
    {
    }
}

public class Child : Parent
{
    // call constructor in the current type
    public Child() : this("abc")
    {
    }

    public Child(string id)
    {
    }

    public override void Foo()
    { 
        // call parent method
        base.Foo();
    }
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
13

The two keywords are very different.

  • this refers to the current instance (not the “current class”). It can only be used in non-static methods (because in a static method there is no current instance). Calling a method on this will call the method in the same way as it would if you called it on a variable containing the same instance.

  • base is a keyword that allows inherited method call, i.e. it calls the specified method from the base type. It too can only be used in a non-static method. It is usually used in a virtual method override, but actually can be used to call any method in the base type. It is very different from normal method invocation because it circumvents the normal virtual-method dispatch: it calls the base method directly even if it is virtual.

Timwi
  • 65,159
  • 33
  • 165
  • 230
9

Darin is right on. An example may also help. (There wasn't an example when I initially posted. Now there is.)

class Base {

    protected virtual void SayHi() {
        Console.WriteLine("Base says hi!");
    }

}

class Derived : Base {

    protected override void SayHi() {
        Console.WriteLine("Derived says hi!");
    }

    public void DoIt() {
        base.SayHi();
        this.SayHi();
    }
}

The above prints "Base says hi!" followed by "Derived says hi!"

Josh
  • 68,005
  • 14
  • 144
  • 156
1

"this" keyword points to the address of current object. we can use "this" keyword to represent current object (of current class).

Where as "base" keyword represent to "Parent class"
So if you want to use/call function of parent class you can use "base" Keyword.
base is very useful in function overriding to call function of parent class.

shA.t
  • 16,580
  • 5
  • 54
  • 111
Dr. Rajesh Rolen
  • 14,029
  • 41
  • 106
  • 178
0

this refers to any object that is currently being used. Base refers to a base class generally speaking.

If the object is of the base then in that case this can refer to the base object also.

ckv
  • 10,539
  • 20
  • 100
  • 144
0

this refers to the current class instance.

base refers to the base class of the current instance, that is, the class from which it is derived. If the current class is not explicitly derived from anything base will refer to the System.Object class (I think).

shA.t
  • 16,580
  • 5
  • 54
  • 111
Andrew Cooper
  • 32,176
  • 5
  • 81
  • 116
0

base - is used to access members of the base class from within a derived class

this - refers to the current instance of the class and inherited

class BaseClass
{
    public string BaseAttr { get; set; }
}
class A : BaseClass
{
    public string Attr { get; set; }
    public void Method()
    {
        this.Attr = "ok";
        this.BaseAttr = "base ok";
        base.BaseAttr = "ok";
        base.Attr = "unavailable"; //!
    }
}
-1

lets say you have code like this

class B extends A {

    public B () {
        // this will refer to the current object of class B
        // base will refer to class A
    }

}

Note: The syntax of code is in java but it is self explanatory.

Faisal Feroz
  • 12,458
  • 4
  • 40
  • 51
-1

If you use this.function() in a class, the will get an error due to stackoverflow.

  • 1
    What language do you mean? This is not always the case. This error can usually occur when creating from the same object inside the constructor in C#. – Ramil Aliyev 007 Feb 14 '21 at 08:41