» Why are static classes considered “classes” and “reference types”?
Why are static classes considered “classes” and “reference types”?
|May 6, 2010
Posted by forumadmin
I’ve been pondering about the C# and CIL type system today and I’ve started to wonder why static classes are considered classes. There are many ways in which they are not really classes:
- A “normal” class can contain non-static members, a static class can’t. In this respect, a class is more similar to a struct than it is to a static class, and yet structs have a separate name.
- You can have a reference to an instance of a “normal” class, but not a static class (despite it being considered a “reference type”). In this respect, a class is more similar to an interface than it is to a static class, and yet interfaces have a separate name.
- The name of a static class can never be used in any place where a type name would normally fit: you can’t declare a variable of this type, you can’t use it as a base type, and you can’t use it as a generic type parameter. In this respect, static classes are somewhat more like namespaces.
- A “normal” class can implement interfaces. Once again, that makes classes more similar to structs than to static classes.
- A “normal” class can inherit from another class.
It is also bizarre that static classes are considered to derive from System.Object. Although this allows them to “inherit” the static methods Equals and ReferenceEquals, the purpose of that inheritance is questionable as you would call those methods on object anyway. C# even allows you to specify that useless inheritance explicitly on static classes, but not on interfaces or structs, where the implicit derivation from object and System.ValueType, respectively, actually has a purpose.
Regarding the subset-of-features argument: Static classes have a subset of the features of classes, but they also have a subset of the features of structs. All of the things that make a class distinct from the other kinds of type, do not seem to apply to static classes.
Regarding the typeof argument: Making a static class into a new and different kind of type does not preclude it from being used in typeof.
Given the sheer oddity of static classes, and the scarcity of similarities between them and “normal” classes, shouldn’t they have been made into a separate kind of type instead of a special kind of class?
More Related Questions
- In C#, why is String a reference type that behaves like a value type? A String is a reference type even though it has most of the characteristics of a value type such as being immutable and having == overloaded to compare the text rather than making sure […]
- Casting vs using the as keyword in the CLR I'm learning about design patterns and because of that I've ended using a lot of interfaces. One of my "goals" is to program to an interface, not an implementation.
What I've found is […]
- Why does this (null || !TryParse) conditional result in "use of unassigned local variable"? The following code results in use of unassigned local variable "numberOfGroups":
if(options.NumberOfGroups == null || !int.TryParse(options.NumberOfGroups, out […]
- Where to put constant strings in C++: static class members or anonymous namespaces I need to define some constant strings that will be used only by one class. It looks like I have three options:
Embed the strings directly into locations where they are used.
Define them […]
- Allocated Bytes and Final Heap Bytes proper ratio ? or Any logic to measure or compare allocating memory and heap final size? Before any questions / explanations from my side, please forgive me if I wasn't clear in the question title.
I just started using the CLRProfiler 64bit that can be downloaded from […]
- Do I understand this MSIL code correctly? I have the following code in C#
private static void Main()
int x = 5;
int y = 100;
Console.WriteLine(y + ", " + x);
And I'm reading the IL code, I've […]
- .Net 4.0 crashes in clr.dll on Windows Server 2008 x86 We have a Windows Application that runs day in day out at several locations. Now though they've started crashing, they don't even generate an error on crash (should be catched by NLOG), […]
- In C#, how do I check if a type is a subtype OR the type of an object? To check if a type is a subclass of another type in C#, it's easy:
typeof (SubClass).IsSubclassOf(typeof (BaseClass)); // returns true
However, this will fail:
- C++ singleton vs. global static object A friend of mine today asked me why should he prefer use of singleton over global static object?
The way I started it to explain was that the singleton can have state vs. static global […]
- Creating method dynamically, and executing it Background:
I want to define few static methods in C# , and generate IL code as byte array, from one of these methods, selected at runtime (on client), and send the byte array over […]