Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question
    
You should add your answer as an actual answer to the question, instead of having it into the question. –  Yannis Rizos 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
add comment

3 Answers 3

up vote 18 down vote accepted

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.

share|improve this answer
    
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
    
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
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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