I always used to consider structures as some sort of lesser privileged things, or something with lesser features. Maybe because of the OOP concepts blowing everything into Classes.
From the little amount of exposure to C#, I understand that Setting a class static, ensures that all its members & functions are static. Also we cannot have a constructor to initialize that class as there is only a single instance.
public static struct mystruct
{
public static int a;
}
I was pointed out right here at Stack overflow that this is a wrong method. Can someone elaborate.
I got the appropriate error saying "static is not valid for this item" when I created a new cs file & compiled it in console. Strangely when I added this to an existing working project to see if the compiler would complain but to my surprise it doesn't. Any reasons for this??
A static class is just a container for static members (of any kind - fields, events, properties, and most commonly methods).
A static struct would be exactly the same, so wouldn't provide any advantages - but readers might think it had some special meaning. To avoid confusion, it's therefore prohibited. Conceptually it makes just as much sense as a static class, of course - the difference between structs and classes is really in terms of how instances of them behave, and as there would be no instances of static types of either kind, that difference would be moot.
(Not that I was in the design meeting where this was decided, of course. Eric Lippert may well be able to find some notes about it. The above is just an educated guess. The annotated C# 3 spec is silent on the matter, as far as I can see.)
This doesn't accomplish anything. You'd still have a collection of static methods just like you do with a static class. A struct in C# implies it is a value type instead of a reference type, which has no meaning at the static level.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With