8

In C#, with an inherited class set -- when calling a method should we use keywords 'base.methodname and this.methodname'... irrespective of whether it is a overridden method or not?

The code is likely to undergo changes in terms of logic and maybe some IF-ELSE like conditions may come in at a later date. So at that time, the developer has to be compelled to revisit each line of code and ensure that he/she makes the right choice of which method is being called --- base.methodname() or this.methodname() ELSE the .NET framework will call the DEFAULT (I think its base.methodname()) and the entire logic can go for a toss.

  • You should add your answer as an actual answer to the question, instead of having it into the question. – yannis Feb 7 '12 at 11:27
  • 3
    "My answer is: YES -- its a good practice -use it because that is why those were created for". By that logic, I could equally say "NO -- it's a good practice, use of these keywords is optional for a reason." – pdr Feb 7 '12 at 11:41
27

Using the base keyword is not a question of preference, it's a question of correctness. If you want to call the base class implementation in overridden method, you have to use base. If you don't want to call that, you can't use base.

The this keyword, on the other hand, is a question of preference in most cases. I think it only unnecessarily clutters the code, so I don't use it if I don't have to. Where it's useful is if you have a method parameter with the same name as field (or property). I would use this in such case. But if I'm writing new code, I tend to use naming conventions that avoid this problem.

  • When I write code just for myself or that will be maintained by me or a different senior developer I will omit this as assumed. On larger projects where I will have junior (potentially even new hires), our standard is to use those keywords just so the scoping is immediately evident regardless of the coder's experience level. – Joel Etherton Feb 7 '12 at 13:27
  • 1
    Hi joel... your outlook towards this is spot-on with that of mine. What we are working is an old C# code written in 2001 and sometimes it is a maze out there. Also tomorrow, we are not sure who would the developer be (trainee or experienced) after a couple of years... hence if these keywords are used and somebody goes on to implement overridden-methods (for sake of polymorphism) then that developer would be compelled to revisit each calls of a given method so that the logic does not fail. – MukeshAnAlsoRan Feb 7 '12 at 14:44
  • If I don't override a method, why couldn't I use base anyway on calling it, even though it is semantically wrong? – Deduplicator Nov 6 '18 at 9:49
  • 1
    @JoelEtherton I don't think programming to cater to the "lowest common denominator" like that is a good idea. I would understand that rationale if discussing something significantly more complex, but this is about as basic as it gets. If programmers don't yet understand the scoping of methods (and the implicit this. access of object members, even when not explicitly written), then they have studying to do. That's on them. – Alexander Nov 6 '18 at 18:10
  • @Alexander this depends on the organization in which you're working and the software/maintenance plan you're using. In many applications, the lowest common denominator ends up being a senior developer with 5-10 years of experience. Sometimes the maintenance burden falls to junior developers with little or no practical experience. The "lowest common denominator" is different for every application. I don't expect a fresh fish to understand the nuances of scoping, access and garbage collection. – Joel Etherton Nov 7 '18 at 19:11
5

I would suggest that if you're explicitly calling a base method from a class in which that method is overridden, you have a serious problem with both logic and readability. base should really only appear where calling the base implementation of an overridden method from that overridden method.

Everywhere else, it should be this (if the method isn't overridden then it will go to the base class anyway) and there is nothing wrong with allowing the compiler to infer that.

0

this keyword : There is no hard and fast rule to apply this keyword as it is a personal preference but your code could get cluttered.

Pros and cons of using this keyword in C#

base keyword (re-usability) : There are already answers indicating the use of base. I would like to add a point.

Common functionality for all derived classes would come in the base class and you can call the base method followed by the specific implementation in the method in the derived class achieving re-usability.

0

In addition to mentioned, you can use "this" when calling an extension function.

public static class ExtensionForMyClass {  
public static void SomeFunction (this MyClass inst) {} 
}

You can't do:

SomeFunction();

from inside MyClass body. Only:

this.SomeFunction();

There is no real reason to create static extensions for the class you are making. The one scenario when it was useful to me is when I had extension functions based on the interface my class is implementing, not on the class itself.

0

I would recommend dropping the this. prefix, except in cases where it adds symmetry. E.g.:

public class Foo: IComparable {
    int value1, value 2;

    public int CompareTo(object _that) {
        if (_that == null) return 1;
        if (Object.ReferenceEquals(this, _that)) return 0;

        Foo that = (foo) _that;
        var result = this.value1.CompareTo(that.value1)
        if (result != 0) return result;

        result = this.value2.CompareTo(that.value2)
        if (result != 0) return result;

        return 0
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.