tags:

views:

160

answers:

2

I was browsing the colors class's source in reflector and it's just a sealed class. But all members are static. So why would anyone create a copy of the Colors class?

It lies inside:

System.Windows.Media.Colors
+8  A: 

A static class is just a sealed abstract class with private constructor no constructors (as in, no constructors at any accessibility level, not even ones generated by the compiler) [fixed per Eric's comment]. The C# keyword static is simply a shorthand for that, and also forces you to make all members static, but for API clients it's exact same thing.

Given that work on WPF (then Avalon) began before .NET 2.0 was released, it could be that this particular class was written before static class appeared. Or perhaps the author was simply unaware of that language feature.

Pavel Minaev
Your first paragraph is not correct. A static class is *not* implemented as a sealed class with a private constructor. Rather, a static class is a *sealed abstract class with no constructors*.
Eric Lippert
Thank you, will fix it.
Pavel Minaev
As far as I know, a static class can have a private default constructor (parameterless) that can initialize any static members if needed and CLR guarantees to call it once per AppDomain before any member or the type itself is referenced.
Chansik Im
To clear up a potential misunderstanding: Chansik Im is referring to the *static constructor* (.cctor), Eric Lippert is referring only to *instance constructors* (.ctor).
Timwi
+2  A: 

For me, Colors.AliceBlue and Colors.AntiqueWhite (the first and the second static property of Colors class :)) should be difference instances of Color objects instead of different types.

public static Color AliceBlue {get;}

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

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

One can argue that different colors should be different types (arguably static as well) instead of different instances of the Color class. Even with this case, Color should not be a static class because you want to have a base Color class for individual Color classes and making the base Color class static automatically makes it sealed.

One may still want to argue that Color should be a namespace and individual colors should be static classes. I will simply ask them to imagine what the API will look like again :)

Chansik Im
He's not talking about `Color` class (which is also there, and isn't static). He's talking about `Colors` class, which exists solely to provide members like `Red` and `Green` - it's a separate class in WPF.
Pavel Minaev
Oh... I see. Thanks for your comment. I got confused by taking the original question literally. In this case, I agree that Colors could have been a static class.
Chansik Im
Yeah sorry, I meant the Colors class, now edited it.
Joan Venge