Why isn't the .NET Colors class static?

I was looking at the source of the colors in the reflector and it is just a sealed class. But all the participants are static. So why would anyone create a copy of the Colors class?

It lies inside:

System.Windows.Media.Colors

      

+2


source to share


3 answers


A static class is just a closed abstract class with a private constructor with no constructors (eg no constructors at any accessibility level, not even compiler generated) [corrected per Eric's comment]. The C # keyword static

is just shorthand for this and also forces you to make all elements static, but for API clients it is exactly the same.



Considering that work on WPF (then Avalon) started before .NET 2.0 was released, it may be that this particular class was written before static class

. Or perhaps the author simply did not know about this language feature.

+8


source


For me, Colors.AliceBlue

and Colors.AntiqueWhite

(the first and second static property of the Colors class :)) should be delimited instances of Color objects instead of different types.

public static Color AliceBlue {get;}

Note that a static class is a private class that only contains static members and cannot be instantiated using the new keyword as it can only have its own default constructor (at least in C #).



Imagine you are creating an API that takes a color as an argument, what would it look like if Color is a static class?

It could be argued that different colors should be of different types (possibly static), and not different instances of the Color class. Even so, Color doesn't have to be a static class, because you want to have a base Color class for the individual Color classes and make the base class of the class static automatically, which makes it sealed.

One could still argue that Color should be a namespace and individual colors should be static. I'll just ask them to imagine what the API would look like :)

+2


source


Since a class sealed

with all static members and a private constructor represents definite performance improvements due to the way the IL

class is formed to use static

versus the class sealed

, in fact the static class gets a modifier abstract

in IL to prevent a new one from being called where the class doesn't exist sealed

...

See .. This post on msdn social

+1


source







All Articles