开发者

Why is the BitmapImage a sealed class?

开发者 https://www.devze.com 2022-12-19 20:40 出处:网络
I was about to inherit from the BitmapImage class when I noticed it was sealed, I开发者_JS百科\'ve had a quick look around but I can\'t find a reason it is sealed.

I was about to inherit from the BitmapImage class when I noticed it was sealed, I开发者_JS百科've had a quick look around but I can't find a reason it is sealed.

Any body know why this is?

Thanks


BitmapImage is most likely sealed because it has a specific usage scenario in it's design, it is meant to be:

a specialized BitmapSource that is optimized for loading images using Extensible Application Markup Language (XAML).

By making the class sealed, it makes it safer to use from XAML, since the XAML parser will always know exactly what is going to be created.

If you want to write your own, you should be deriving from BitmapSource instead, which is abstract, and intended to be subclassed.


Having a class unsealed is the equivalent of saying "I want people to inherit from this type". This causes people to inherit from the type whether or not it's a good idea (see the numerous StackOverflow questions where people inherit from List<T> as an example).

Since inevitable people will inherit from it, this means you must design a class to be inheritted. This does have consequences and does increase cost in both dev and test time. The author of this type likely did not want to do this work or had reasons for it not being inheritable and decided to seal the type


Especially when developing a class library, unless you've specifically designed for inheritance, your externally exposed classes should be sealed by default. If a user of the class wants to inherit at some point in the future, then it's easy and non-breaking to unseal the class.

If you start out with the class unsealed, you can never go back.

0

精彩评论

暂无评论...
验证码 换一张
取 消