Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I was doing some code review today and came across an old code written by some developer. It goes something like this

public abstract class BaseControl
{
    internal abstract void DoSomething();
}

If you have a derived class within the same assembly, it would work

public class DerivedControl : BaseControl
{
    internal override void DoSomething()
    {
    }
}

But deriving the base class in a different assembly would give compile time error

DerivedControl does not implement inherited abstract member 'BaseControl.DoSomething()

That got me thinking. Why would anyone declare a method as internal abstract ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
563 views
Welcome To Ask or Share your Answers For Others

1 Answer

The original programmer wanted to make a derived control available to client code. But prevent the client from inheriting and messing with the virtual method. That's not a bad idea, it is usually easy to break a base class by overriding a method and doing something like forgetting to call the base class method.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...