Static Keyword in C#

Static has all these interesting semantics around it.

Basics:

  • The static modifier can be used with classes, fields, methods, properties, operators, events, and constructors, but it cannot be used with indexers, destructors, or types other than classes.
  • A constant or type declaration is implicitly a static member.
  • A static member cannot be referenced through an instance. Instead, it is referenced through the type name
  • While an instance of a class contains a separate copy of all instance fields of the class, there is only one copy of each static field.
  • It is not possible to use this to reference static methods or property accessors.

Static Class:

  • If the static keyword is applied to a class, all the members of the class must be static.
  • A static class can be used as a convenient container for sets of methods that just operate on input parameters and do not have to get or set any internal instance fields. For example, in the .NET Framework Class Library, the static System.Math class contains methods that perform mathematical operations, without any requirement to store or retrieve data that is unique to a particular instance of the Math class

 

Static Members / Methods:

 

Static Constructors:

(I once used a static constructor for my web service, when I had to initialize some of the static data in my class.  The other alternative to not using the static constructor was basically to find a static function in a static class which I knew would have gotten called only once when my web service was getting initialized. See Code for pointers)

  • Classes and static classes may have static constructors. Static constructors are called at some point between when the program starts and the class is instantiated
  • A static constructor is only called one time, and a static class remains in memory for the lifetime of the application domain in which your program resides.
  • A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only. It is called automatically before the first instance is created or any static members are referenced.
  • A static constructor does not take access modifiers or have parameters.
  • A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
  • A static constructor cannot be called directly.
  • The user has no control on when the static constructor is executed in the program.
  • A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.
  • Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method.
  • If a static constructor throws an exception, the runtime will not invoke it a second time, and the type will remain uninitialized for the lifetime of the application domain in which your program is running.

Code:

Reference:

  1. static :
  2. static classes and static class members :
  3. static constructors
  4. singleton:
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s