What is the use of 'abstract override' in C#?

前端 未结 7 983
梦如初夏
梦如初夏 2020-11-27 17:44

Just out of curiosity I tried overriding a abstract method in base class, and method the implementation abstract. As below:

public abstract class FirstAbstra         


        
7条回答
  •  伪装坚强ぢ
    2020-11-27 18:11

    I find it really useful for ensuring proper ToString() implementation in derived classes. Let's say you have abstract base class, and you really want all derived classes to define meanigful ToString() implementation because you are actively using it. You can do it very elegantly with abstract override:

    public abstract class Base
    {
        public abstract override string ToString();
    }
    

    It is a clear signal to implementers that ToString() will be used in base class in some way (like writing output to user). Normally, they would not think about defining this override.

提交回复
热议问题