developmentor - a developer services company				  
				      C# Tutorial
   Modules

Static


Goals:

  • Introduce the concept of static fields and methods.
  • Show how to declare static fields.
  • Show how to implement static methods.
  • Show how to access static members.
  • Discuss the three initialization options for static fields: default value, inline initialization, and static constructor.

Overview

The keyword static can be applied to fields and methods. Static fields implement the idea of data that is shared by multiple clients. Static methods are typically either access methods to static data or utility methods that do not need to maintain state across calls.


Static field

Regular fields are often called "instance fields" because each instance of the class gets their own copy. Consider the following Item class containing three instance fields and notice how the objects look in memory.

By contrast, there is only one copy of each static field and the single copy is shared by all objects. Consider the new version of the Item class shown below. The Revenue field has been made static; therefore, there is a single copy generated that is separate from all Item objects.

Client code that needs to use a static field must access it using the class name.

Static fields are used to implement the concept of "shared resource" since the single copy can be shared by multiple clients. In other languages, this role is often filled by global variables. The type designer can choose the access level for a static field: private static fields are shared only by instances of that type while public static fields are shared by all clients.

The memory for static fields is allocated by the CLR when the class is initialized. Conceptually, you can think of this as occurring "at program startup"; however, the exact timing is up to the CLR and in many cases this setup will occur later. The key thing to take away is that the memory for a static field will be available for use even if no objects of that type have been created.


Static field default values

Static fields of a class are set to default values when the class is initialized by the CLR. The default values depend on the type of the field.

Numeric fields such as int, float, double, etc. are set to zero.

Fields of type bool are set to false.

Characters fields are set to the null character (often written '\x0000').

References are set to null. We will discuss references in the module on reference types.


Static variable initializer

Static fields can be initialized inline, that is, at the point of declaration. The official name for this technique is 'static variable initializer'.


Static constructor

The most powerful initialization technique is to provide a static constructor. The static constructor is defined using the keyword static, the class name, and an empty argument list. No return type, access modifier, or parameters are allowed. The CLR invokes the static constructor automatically when the class is initialized. The exact timing of the invocation is not guaranteed, but it will be run after the static variable initializers have been executed.


Static method

A method can be made static by adding the keyword static to its declaration.

A static method must be invoked using the type name.

Static methods are not as powerful as instance methods. In particular, static methods can only access the static parts of their type and are not allowed to access any instance fields or methods. This limitation means static methods are not quite as useful as instance methods; however, static methods do still have their place. Static methods are typically used for 'utility' methods that do not need to maintain state across calls. Several good examples of this application can be seen in the Math and Convert classes from the .NET Framework Class Library. A less common application of static methods is as accessors or manipulators of some 'global' object. The classic example of this is the library Console class where the read and write methods manipulate the console attached to the executing process.

This material is excerpted from the Programming C# course offered by DevelopMentor.