12

What is the use of a static class? I mean what are benefits of using static class and how CLR deals with static classes?

Gerrie Schenck
  • 22,148
  • 20
  • 68
  • 95
Embedd_0913
  • 16,125
  • 37
  • 97
  • 135

9 Answers9

31

A static class simply denotes that you don't expect or need an instance. This is useful for utility logic, where the code is not object-specific. For example, extension methods can only be written in a static class.

Pre C# 2.0, you could just have a regular class with a private constructor; but static makes it formal that you can never have an instance (there is no constructor*, and all members must be static).

(*=see comment chain; you can optionally have a type initializer (static constructor aka .cctor), but you cannot have an instance constructor (aka .ctor)).

MSeifert
  • 145,886
  • 38
  • 333
  • 352
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • static class actually does have constructor. – lubos hasko Feb 23 '09 at 08:12
  • no, it really doesn't. "static class Foo {}" compiles to IL: ".class private abstract auto ansi sealed beforefieldinit Foo extends [mscorlib]System.Object { }" - no ctor. It can *optionally* have a type initializer (.cctor), but that is different to an (instance) constructor (.ctor) – Marc Gravell Feb 23 '09 at 08:14
  • ok, so it doesn't have contructor (you're right) but static types can have optional type initializer which in fact works like constructor. I would say your answer is still misleading if you say there is no contructor without mentioning optional type initializer. – lubos hasko Feb 23 '09 at 08:43
  • Fine - I'll add the word "instance", but I think the comments have more than covered this. – Marc Gravell Feb 23 '09 at 08:47
6

The compilation and metadata model for .net requires that all functions are defined within a class. This makes life somewhat easier and simpler for the reflection api's since the concepts of the owning class and its visibility is well defined. It also makes the il model simpler.

since this precludes free functions (ones not associated with a class) this makes the choice of where to put functions which have no associated state (and thus need for an instance). If they need no state associated with them nor have any clear instance based class to which they can be associated and thus defined within there needs to be some idiom for their definition.

Previously the best way was to define the methods within a class whose constructor was private and have none of the functions within the class construct it.

This is a little messy (since it doesn't make it crystal clear why it was done without comments) and the reflection api can still find the constructor and invoke it.

Thus static classes were allowed, making the intent of the class, that of a place for the definition of static methods, clear to users and to the type system. Static classes have no constructor at all.

ShuggyCoUk
  • 36,004
  • 6
  • 77
  • 101
4

A static class is a language hack to write procedural programs in C#.

Chris Peterson
  • 2,377
  • 1
  • 21
  • 24
2

All members of a static class has to be static members, so, if you forget to add 'static' before any of them your code won't compile, it also makes your code more readable as anyone who will see that the class is static will understand it only contains static members.

The best use for a static class is for utility functions, you could also use them to keep the global methods and data in your application, I use static static classes very often in almost any project.

Waleed Eissa
  • 10,283
  • 16
  • 60
  • 82
2

Static classes are often used to group related global services which you initially don't want to be accessed with an object instance. An example is the Math class in the .Net BCL, which you use directly, e.g., Math.Sqrt(10.0)

robi-y
  • 1,687
  • 16
  • 25
1

I got clear idea from this statements.

To know more about static class. First we must differentiate between static and instance data first.

Each time you create a new instance of a class you get a new copy of the instance data to play with. The instance methods of the class work on the instance data. The instance data is completely independent of the instance data in all other classes and even instances of the same class. If you change a value in one instance it has no impact on the same value in other instances. The bulk of program data is instance data.

Static data is equivalent to global data. Everybody in the program sees the same data. If someone changes the data then everybody else will see the change as well. Static data is useful for sharing information across a program such as database connection strings or log files. There is only one copy of static data in memory in general. (There are exceptions such as when dealing with multiple appdomains).

When you create an instance of a class you are effectively allocating some memory to hold your own copy of the instance data defined by the class. If you create 5 instances of a class then you get 5 separate memory locations where each location has its own copy of the instance data. Each memory block is independent of the others.

If a class has no instance data then it doesn't make any sense to create instances of it. Doing so is harmless but also a waste of time. This is where static classes come in. A static class is a way to identify a class as having no instance data. By marking the class as static you are telling the compiler that all the data in the class is shared across the board. As a result the compiler enforces some rules to make things clear. A static class can only contain static members. A static class cannot be instantiated. A static class must be sealed. The compiler enforces this for convenience of the developers.

A static class is sealed. This is because static classes cannot have per-instance data.

Static methods are defined on the type level not on the instance level. That is why overriding doesn't work with static methods/properties/events...

Static constructor is called once and the static class remains in the memory for the lifetime of the application domain in which your program resides.

Nayas Subramanian
  • 2,269
  • 21
  • 28
1

Static classes are sealed. This may be a useful option to use static for utility classes.

Canavar
  • 47,715
  • 17
  • 91
  • 122
0

Reference: http://msdn.microsoft.com/en-us/library/79b3xss3(VS.80).aspx

1.First of all u cannot create the instance for static classes

  1. If the class declared as static, member variable should be static for that class

3.Sealed [Cannot be Inherited]

4.Cannot contains Instance constructor

5.Memory Management

Example: Math calculations (math values) does not changes [STANDARD CALCULATION FOR DEFINED VALUES]

Vicky
  • 819
  • 2
  • 13
  • 30
-3

In a class we declare a function as static,only if that function is not associated to any objects. We should not use "this" operator in that static function, because "this" operator will refers to object which invoke the function. Eg: Consider a class named Employee has few variables like Name, Age, Department, in this Employee class i am going to add a function called getSimilarNames() that will show How many employees having similar names. This function need not to be associated to any Employees. So I declare this function as a Static.If a class that only contains static functions, we declare that class is a static class. The Static function improves performance.

  • The question is about class, not functions. Almost all your sentences are too vague and confusing. Your example is false : getSimilarNames() DOES require access to the name. getSimilarNames(string ThisName) might be static. And call it GetIdenticalNamedCount since this is a count that is returned. And no, static class might have const members also, and no, static functions do not improve performance. Nice post. – GameAlchemist Feb 19 '12 at 18:38