Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
590 views
in Technique[技术] by (71.8m points)

c# - internal abstract methods. Why would anyone have them?

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...