Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# StyleCop - Using "this." prefix for base class members like current class members or not?

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.

like image 529
Amir Karimi Avatar asked Sep 07 '10 22:09

Amir Karimi


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


2 Answers

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.

like image 67
Timwi Avatar answered Oct 05 '22 09:10

Timwi


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.

like image 42
Scott Dorman Avatar answered Oct 05 '22 07:10

Scott Dorman