C# override Behavior

by Chad 5. June 2009 18:42

I would like to begin this post by first thanking Brandon King and the Ozarks Dot Net User Groupfor inviting me to speak at their meeting last night. They have an enthusiastic group of guys and I really enjoyed the dialog during my presentation. I’d also like to give a shout-out for SharePoint Saturday Ozarks. Mark Rackley has worked very hard to put together an impressive list of speakers offering SharePoint and .NET tracks, swag, and food all for the low price of FREE. SharePoint Saturday Ozarks will be in Harrison, AR on Saturday, July 18th.

I Was Wrong

This post is prompted by an incorrect statement that I made at last night’s presentation. I’m pretty good about qualifying things that I say if I’m unsure about the validity of what I’m saying.  It usually goes something like this “I’m pretty sure [questionable statement].” or “I don’t know, but I’d assume [most likely incorrect statement].”  But last night during a side discussion about the this and override keywords in C#, I confidently gave false information. This isn’t the first (or last) time that I’ve been wrong, but since I gave my explanation with such confidence and in front of a group of professional developers, I feel morally obligated to cancel my misinformation with search engine indexed truthiness.

The Truth About C# override Behavior

Override methods are not considered as declared on a class, they are new implementations of a method declared on a base class.” – MSDN

What this means is that even though you can invoke the Base class’ implementation from a Derived class using base.Method();, this.Method(); when called from the Base class will still invoke the overridden implementation in the Derived class. I mistakenly thought that, from within the Base Class, that a call to this.Method(); would invoke the Base implementation and Method(); without the this keyword would invoke the Derived implementation.

At first this seemed a bit funny to me until I considered how the compiler resolves method names. Consider the following code example:

public class Derived : Base
{
    public override void DoWork(int param) { }
    public void DoWork(double param) { }
}

int val = 5;
Derived d = new Derived();
d.DoWork(val);  // Calls Derived.DoWork(double).

The compiler is trying to match the most specific method based on the type of the object. In this case it first searches Derived for a compatible method. Because int can be implicitly cast to double, the compiler matches on DoWork(double). Since the compiler considers the override DoWork(int) as implementation on the Base class, it doesn’t match the more specific signature because it didn’t look any further. To invoke the overridden behavior you must first cast the instance d to Base.

Base d2 = (Base)d;
d2.DoWork(val);  // Calls Derived.DoWork(int).

Now think about exactly what is happening when you use the this keyword from within an instance of Base. The this keyword is essentially a variable of the instance as the current type - Base. The compiler is doing the same method name matching on the this keyword as it does on the d2 variable so it makes sense that this.DoWork(5); would invoke the overridden implementation.

Now I know. :)

Tags: ,

.NET-Basics

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

About the author

Chad

Meeeee!!!

Hi, my name is Chad Boschert and I'm a software developer in Springfield, Missouri. I've been developing .NET applications in C# since 2002.

Recent Comments

Comment RSS

Calendar

<<  May 2013  >>
MoTuWeThFrSaSu
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

View posts in large calendar