Why does resharper prefer to not hide a property from an abstract class inside another abstract class?

后端 未结 2 710
轮回少年
轮回少年 2021-01-22 14:53

Why does re-sharper want me to not hide a property from an abstract class? It wants me to use \'new\', but is that always preferable? It seems to imply that hiding a variable s

2条回答
  •  日久生厌
    2021-01-22 15:34

    If you don't want the outside world to be able to access the base.property, it should be protected. If it is, you shouldn't be redefining it in a derived class. If it shouldn't be available to the derived class at all, make it private.

    It's suggesting new to prevent ambiguity, but in reality you shouldn't be there to begin with.

    The reason is that if I do this:

    var item = new verySpecificClass();
    var val = item.commonProperty;
    
    var basic = (baseClass)item;
    if(val == basic.commonProperty)
    

    I'm going to get different results than what a reasonable programmer would expect.

提交回复
热议问题