Calling base for an inherited method?

  • Thread starter Thread starter Dave Veeneman
  • Start date Start date
D

Dave Veeneman

I'm using inheritance more than I used to, and I find myself calling a lot
of base class methods. I generally call a base method from a dreived class
like this:

this.MyMethod();

I'm finding it somewhat confusing when I look at the code later, because I
expect to find a method called MyMethod() in the derived class.

I think C# would let me call a base class method from a derived class like
this:

base.MyMethod();

This would make it clear that the method being called is a base class
method. From the standpoint of clarity, I like it.

But, are there other reasons I couldn't call a base class method like that?
Reasons why I shouldn't? Thanks.

Dave Veeneman
Foresight systems
 
Dave Veeneman said:
I'm using inheritance more than I used to, and I find myself calling a lot
of base class methods. I generally call a base method from a dreived class
like this:

this.MyMethod();

I'm finding it somewhat confusing when I look at the code later, because I
expect to find a method called MyMethod() in the derived class.

I think C# would let me call a base class method from a derived class like
this:

base.MyMethod();

This would make it clear that the method being called is a base class
method. From the standpoint of clarity, I like it.

But, are there other reasons I couldn't call a base class method like that?
Reasons why I shouldn't? Thanks.

Well, it depends whether you want to call the overridden version if
there is one. If you absolutely know when you write the code that you
don't want to call an overridden version, call base.MyMethod(). If you
want to use whatever version has been provided by the class, call
this.MyMethod() or just MyMethod(). I personally rarely use
base.MyMethod() outside code which is itself overriding MyMethod.
 
I am assuming MyMethod is something defined in the base class but *not*
overridden in the inheriting class? In that case, these are the same.

However, if MyMethod is overridden in the inheriting class, then these 2
things are completely different. The first one calls the overridden version,
and the second one calls the base version.

So you want to be careful in how you typically call these methods, to make
sure you are calling the one you really want.

In the case where MyMethod may or may not be overridden, it seems that
always calling this.MyMethod is safest. If there is a version in the
inheriting clas - that will get called. If not, the base version will get
called. So I would only use 'base' to prefix things where you explicitly
want to call the base class's version for whatever reason, regardless of
whether or not there is a version in the inheriting class.
 
The one place you may get into problems will be with future development.
Assume that you have class A and B where B is a child class of A. In B, you
have several calls to base.MyMethod(), which is defined in A. Later on, you
come back to your code and add an override of MyMethod() in B. If you
forget to change your code, all of the rest of B will be calling the A
version of MyMethod() instead of the one you defined.

Like wise, if someone comes along and declares a class C that is a child of
B you will get some unexpected behavior. If they wish to perform some
action every time that MyMethod() is called, they would expect to be able to
override MyMethod() in their class. However your code is calling
base.MyMethod() so C will not be able to intercept any of hte calls that B
makes.

--
Jared Parson [MSFT]
(e-mail address removed)

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
 
Dave Veeneman said:
I'm using inheritance more than I used to, and I find myself calling a lot
of base class methods. I generally call a base method from a dreived class
like this:

this.MyMethod();

that is not how you call a base class method. it won't work if you override or hide the base method with a new definition in your derived class.
I'm finding it somewhat confusing when I look at the code later, because I
expect to find a method called MyMethod() in the derived class.

I think C# would let me call a base class method from a derived class like
this:

base.MyMethod();

This would make it clear that the method being called is a base class
method. From the standpoint of clarity, I like it.

But, are there other reasons I couldn't call a base class method like that?

no. it's perfectly valid syntax.
 
Marina said:
I am assuming MyMethod is something defined in the base class but *not*
overridden in the inheriting class? In that case, these are the same.

I'm not disagreeing with you, but it's worth just pointing out that it
could also be overridden in a class which derives from the one in which
the call is made, in which case it still makes a difference. You need
to know in advance which version you want to call if and when the
method is overridden in either a derived class or at a later date in
this class.
So you want to be careful in how you typically call these methods, to make
sure you are calling the one you really want.
Agreed.

In the case where MyMethod may or may not be overridden, it seems that
always calling this.MyMethod is safest. If there is a version in the
inheriting clas - that will get called. If not, the base version will get
called. So I would only use 'base' to prefix things where you explicitly
want to call the base class's version for whatever reason, regardless of
whether or not there is a version in the inheriting class.

Absolutely.
 
Back
Top