Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a "static" class?

In C# what is the difference between:

public static class ClassName {} 

And:

public class ClassName {} 
like image 381
Jeremy H Avatar asked Mar 29 '09 18:03

Jeremy H


People also ask

What does static class means?

A static inner class is a nested class which is a static member of the outer class. It can be accessed without instantiating the outer class, using other static members. Just like static members, a static nested class does not have access to the instance variables and methods of the outer class.

What is static class in Java?

In Java, static is a keyword used to describe how objects are managed in memory. It means that the static object belongs specifically to the class, instead of instances of that class. Variables, methods, and nested classes can be static. Think of a class for a book.

What is class and static class?

A static class is similar to a class that is both abstract and sealed. The difference between a static class and a non-static class is that a static class cannot be instantiated or inherited and that all of the members of the class are static in nature.

What are static classes in C#?

A static class in C# is a class that cannot be instantiated. A static class can only contain static data members including static methods, static constructors, and static properties. In C#, a static class is a class that cannot be instantiated.


1 Answers

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"):

  • Static classes always derive from object. You can't specify a different base type, or make the static class implement an interface.
  • Static classes can't have any instance members - all variables, methods etc must be static.
  • Static classes can't declare any instance constructors and the compiler doesn't create a parameterless constructor by default. (Before static classes came in C# 2.0, people would often create an abstract class with a private constructor, which prevented instantiation. No need here.)
  • Static classes are implicitly abstract (i.e. they're compiled to IL which describes an abstract class) but you can't add the abstract modifier yourself.
  • Static classes are implicitly sealed (i.e. they're compiled to IL which describes an sealed class) but you can't add the sealed modifier yourself.
  • Static classes may be generic.
  • Static classes may be nested, in either non-static or static classes.
  • Static classes may have nested types, either non-static or static.
  • Only static, top-level non-generic classes can contain extension methods (C# 3.0).
like image 98
Jon Skeet Avatar answered Sep 30 '22 22:09

Jon Skeet