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

Categories

0 votes
591 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 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...