0

I have implemented a method in the base class as following:

  class A
    {
        protected void f1()
        {

        }
    }
    class A1 : A
    {
        public void f2()
        {
           //Simple Calling
            f1();
           //Calling using base pointer
            base.f1();
        }

    }

What is the difference between the calling simply and calling using a base pointer ? What are the advantages of either of the ways?

Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126
user1672097
  • 361
  • 1
  • 4
  • 12
  • 2
    Quick tip - pointers do not exist in C# by default (unless you use unsafe code), you're basically using the `base` keyword. – Jason Evans Aug 22 '13 at 10:15

7 Answers7

3

In your example there is no difference. However, consider situation when f1 is virtual and it has another implementation in A1 class:

class A
{
    protected virtual void f1()
    {
        Console.WriteLine("A");
    }
}

class A1 : A
{
    public void f2()
    {
       //Simple Calling - prints `A1`
        f1();
       //Calling using base pointer - prints `A`
        base.f1();
    }

    protected override void f1()
    {
        Console.WriteLine("A1");
    }
}

f1() is different than base.f1() then. The same situation appears when you use new keyword to hide base implementation within derived class:

protected new void f1()
{
    Console.WriteLine("A1");
}
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
1

In this case, none. But imagine this:

class A 
{ 
    public virtual void F() 
    {
    } 
}
class B : A 
{ 
   public override void F() 
   { 
       Console.WriteLine("B"); 
   } 
   public void F2() 
   { 
       F(); /*output: B*/ 
       base.F() /*no output*/ 
   } 
}

That's where base starts to come in useful.

Microsoft DN
  • 9,706
  • 10
  • 51
  • 71
It'sNotALie.
  • 22,289
  • 12
  • 68
  • 103
1

The difference between this.f1() (or simply f1()) and base.f1() becomes relevant when you override a virtual method:

class A
{
    public virtual void F()
    {
        Console.WriteLine("A");
    }
}

class B : A
{
    public override void F()
    {
        Console.WriteLine("B");
    }

    void Test()
    {
        F(); // Prints "B"
        this.F(); // Prints "B"
        base.F(); // Prints "A"
    }
}
Magnus Grindal Bakken
  • 2,083
  • 1
  • 16
  • 22
1

It's only useful if you have overloaded/shadowed a method defined in the base class.

class A1 : A
{
    public void f2()
    {
       //Simple Calling
        f1();
       //Calling using base pointer
        base.f1();
    }

    protected new void f1()
    {
        // I won't be called
    }

}
xanatos
  • 109,618
  • 12
  • 197
  • 280
1

Also useful when you want to extend the functionality of a base method, but don't want to replicate it:

class A
{
    public virtual void F()
    {
        Console.WriteLine("A");
    }
}

    class B : A
    {
        public override void F()
        {
            base.F();
            Console.WriteLine("B");
        }

        void Test()
        {
            F(); // Prints "A B"
        }
    }
Paddy
  • 33,309
  • 15
  • 79
  • 114
0

The base keyword is used to refer to the base class when chaining constructors or when you want to access a member (method, property, anything) in the base class that has been overridden or hidden in the current class. For example,

class A {
    protected virtual void Foo() {
        Console.WriteLine("I'm A");
    }
}

class B : A {
    protected override void Foo() {
        Console.WriteLine("I'm B");
    }

    public void Bar() {
        Foo();
        base.Foo();
    }
}

With these definitions,

new B().Bar();

would output

I'm B
I'm A

Reference

Community
  • 1
  • 1
Prabhakaran Parthipan
  • 4,193
  • 2
  • 18
  • 27
0

if you override f1, base its needed to differentiate between them.

class A
{
    protected virtual void f1() {   }
}
class A1 : A
{
    protected override void f1() {   }

    public void f2()
    {
       //Simple Calling
        f1();                 <--- this will call to overrided f1 in this class
       //Calling using base pointer
        base.f1();
    }

}
Blau
  • 5,742
  • 1
  • 18
  • 27