In C# what is the difference between:
public static class ClassName {}
And:
public class ClassName {}
Firstly, a comment on an answer asked about what "static" means. In C# terms, "static" means "relating to the type itself, rather than an instance of the type." You access a static member (from another type) using the type name instead of a reference or a value. For example:
// Static method, so called using type name
Guid someGuid = Guid.NewGuid();
// Instance method, called on a value
string asString = someGuid.ToString();
Now, static classes...
Static classes are usually used as "utility" classes. The canonical example is probably System.Math
. It doesn't make sense to create an instance of math - it just "is". A few rules (both "can" and "can't"):
object
. You can't specify a different base type, or make the static class implement an interface.abstract
modifier yourself.sealed
modifier yourself.