views:

151

answers:

5

StyleCop has a rule about using "this." prefix to calling class members (SA1101).

Is this rule holds true about a member (for example a method) of a class which is inherited from its base class.

Example:

class BaseClass
{
    protected void F1()
    {
        ...
    }
}    

class ChildClass : BaseClass
{
    protected void F2()
    {
        ...
    }

    protected void F3()
    {
        this.F2(); // This is correct acording to SA1101

        // F1 is a member of base class and if I dont put this prefix, stylecop will not show any message.
        this.F1(); // Is this correct?
        F1();      // Or this?
    }
}

I know this is just for better readability.

A: 

I like to use base. base.F1() for your case. That prevents accidently referencing a local variable, and is a visual reminder of where the member came from.

jwetzel1492
It's nice but in this case we will encounter another rule in stylecop (SA1100: The call to GetItem should only use the 'base.' prefix if the item is declared virtual in the base class and an override is defined in the local class. Otherwise, prefix the call with this rather than base)
amkh
Using “base” would be really bad practice if it isn’t the base method of the current virtual method you’re calling. The base method could be made virtual and you (or someone else in the hierarchy above you) might want to override it, and then your base call would call the wrong method.
Timwi
That's really bad practice. You should use base only to specify that you want the base method *instead of* one defined in that class. Calling it anywhere other than in an over-ride or hider is dodgy at best. It's prone to error and would confuse others looking at the code as they'd interpret it as being used reasonably and be confused as to why they couldn't find the over-ride or hiding method.
Jon Hanna
A: 

I believe that is correct since the rule holds for all methods regardless of whether they are defined on the base or not. Personally I am not a huge fan of this rule so I just disable it.

Nathan
I agree with you but is there anyone or anything like stylecop which explain about that?
amkh
A: 

These questions will spawn arguments from the pit of nonsense sometimes.

Usually dev teams have a standard for this - which should be followed.

However, general rule is base for accessing base functionality, this for calling any function in the current level. Even if it's for the sole reason of readability, it's a good practice to get into.

Ryan Ternier
Thanks, but I wrote comment to jwetzel1492 about rule SA1100 which we will encounter when using 'base' keyword in this case.
amkh
Using “base” would be really bad practice if it isn’t the base method of the current virtual method you’re calling. The base method could be made virtual and you (or someone else in the hierarchy above you) might want to override it, and then your base call would call the wrong method.
Timwi
A: 

The documentation for StyleCop Rule SA1101 actually mentions this:

A violation of this rule occurs whenever the code contains a call to an instance member of the local class or a base class which is not prefixed with ‘this.’.

(emphasis added by myself). So yes, the rule requires this. on every access to an instance member, irrespective of whether that member is in the local class or inherited from a base class.

Timwi
Thanks, It's interested! StyleCop will not show any message when you fogot "this." prefix for base members.
amkh
+1  A: 

If you think about the rules for object inheritance, even though F1() is actually declared on BaseClass it is inherited by ChildClass so it is valid to call it as this.F1(). This is what StyleCop is telling you to do. By prefixing the call with this, it becomes unambiguous that you are calling the F1() instance method of the current runtime instance of the class.

In fact, calling it as F1() or this.F1() are actually synonymous, but the meaning/intent becomes clearer when using the this prefix.

You should not use the base prefix here at all (even though it will compile) because F1() is not virtual and being overridden in ChildClass. The only reason to use the base prefix is when you have overridden a virtual base class member and want to explicitly call that base class member from within the overriding member. If you did actually use the base prefix without F1() being virtual everything would actually work until you made F1() virtual and added an override in ChildClass. At that point, any calls to base.F1() would continue calling BaseClass.F1() and not the new override in ChildClass.

Scott Dorman