Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do WPF shipped classes have internal virtual methods?

Tags:

c#

wpf

Today I have taken a look at the implementation of the Thumb control in order to learn how it works. What struck me is, that there are 3 methods which seem to override internal virtual parent methods, namely

internal override int EffectiveValuesInitialSize {get;}
internal override DependencyObjectType DTypeThemeStyleKey {get;}
internal override void ChangeVisualState(bool useTransitions);

I am not so much interested in what these methods do specifically, but rather what could be valid reasons to allow overriding them only internally (Microsoft). I couldn't find any obvious information about this.

Why should the ability to extend some functionality of Control/FrameworkElement/UIElement/etc. not be available to the general user? To me the only plausible explanation seems to be, that this functionality is designed for specific child classes of WPF, which is somehow a violation of OOP principles, isn't it?

I silently assume that it's not just that Microsoft can say: "we can do something with our framework that you can't, tee-hee".

Follow-up: most of the internal stuff in Thumb was somehow related to visual states. So I have come to the conclusion that I can safely get rid of that part of the interface in the new class I am creating, and implement my own visual states if I ever need them.

like image 249
oliver Avatar asked Apr 04 '18 17:04

oliver


People also ask

Why do we need virtual methods in C#?

Virtual methods allow subclasses of the type to override the method. They are used to implement run time polymorphism or late binding. It should be noted that virtual or abstract members of a class cannot be declared as private.

Is it mandatory to override virtual method in C#?

When the method is declared as virtual in a base class, and the same definition exists in a derived class, there is no need for override, but a different definition will only work if the method is overridden in the derived class. Two important rules: By default, methods are non-virtual, and they cannot be overridden.

Should all methods be virtual?

No, you should not mark all methods as virtual. You should consider how your class could be inherited. If the class should not be inherited, then mark it sealed and obviously the members should not be virtual. If your class is likely to be inherited, you really should maximize the ability to override the behavior.

Can you override non-virtual method?

You cannot override a non-virtual or static method. The overridden base method must be virtual , abstract , or override . An override declaration cannot change the accessibility of the virtual method. Both the override method and the virtual method must have the same access level modifier.


1 Answers

I can't speak to WPF specifically, but as someone who has written (not officially supported) libraries for Microsoft:

1) It doesn't violate OOP principles. NOT hiding methods from external assemblies they have no reason to know about would violate them. (Improper encapsulation)

2) It prevents intellisense from showing a bunch of junk that you don't need to use the class. (which is really part of #1, but I'll call it out specifically, because it's a big deal)

3) It makes testing much easier.

I'm sure there are other reasons for it I'm not thinking of.

In my particular case I had a base class wrapping a C Win32 API, and each derived class had to handle one of three major uses of the API. Particular methods had to be overriden to do that. Absolutely no reason for an end user of these classes to see that junk.

Also I'll note that in my particular case I also found internal objectionable because I also wanted protected so that a class would have to be internal to the assembly AND derived from the base class to access or override the method. protected internal allows either OR, and private protected was not available at the time. I ended up finding some alternate approach I can no longer recall. Point being, I suspect--but am not sure--internal is probably improper encapsulation in the sense that private protected would have been preferred had it been available. (Although I understand the CLR has always supported it. Just not C# or VB.NET)

Last note: Reading your comments, I may have missed the real drift of your question. The overriden methods in Thumb are probably handling stuff for a base class that there is no intention for you to derive from directly. Either because doing so would be FAR too complicated, or because every possible derivation is handled by the derived classes. (Such as a Shape base class for an API for an engine that only handles rectangles and triangles. Provided Rectangle and Triangle, you have no reason to derive from Shape. And at that point, there's probably stuff that Rectangle and Triangle handle through virtual methods that you have no possible reason to know about.) The other possible thing is methods that are handled fine in the base class which can be derived from, but can be overridden for optimization purposes...but only internally because the exposed API surface area just isn't worth it.

like image 185
zzxyz Avatar answered Nov 11 '22 18:11

zzxyz