C# static field

tutorial and example
tutorial and example

In C#, static filed is the one which belongs to the class not instance hence we don’t need to create instance to access static field. The memory is assigned only once to a static field at the time of class loading which is common among all the objects.

Advantages:

  1. We don’t need to create object to access static data that’s why it is memory efficient.
  2. The data which is common among all the objects is made static to avoid redundancy of data.

Static Filed:

A filed that is declared static is called static filed. Static field gets memory only once at the time of class loading. The common property among all the objects is referred by static field such as company name in case of employees, college name in case of students.

C# Static Filed Example:

Let’s see a simple example of static field in C#.

Output

C# Static field Example: Counting Number of instances

Output