static keyword...?

FlyBoy

Centurion
Joined
Sep 6, 2004
Messages
106
hey...i would like to know what are the uses of the static keyword when using it with properties....

when using it on class or structs , i know that i dont need to initialise the class or struct to use the static method....but when it comes to properties....i have no idea.
any explanation would be great...
10x in advance
 
there are many uses. . . one that come to mind is creating a singleton instance of a class, forcing all clients to use the same instance -


PHP:
public class FactoriedClass 
{
 static FactoriedClass _singletonInstance;
 
 private FactoriedClass()
 {
 }
 
 public static FactoriedClass Instance
 {
  get
  {
   if ( _singletonInstance == null )
	_singletonInstance = new FactoriedClass();
   return _singletonInstance;
  }
 }
}

usage

FactoriedClass fc1 = FactoriedClass.Instance;
FactoriedClass fc2 = FactoriedClass.Instance;

fc1 and fc2 are the same instance of Factoried class.

Factoried class is only accessible via the Instance property, you cant 'create' one via 'new'

look through help for examples of where the .NET framerwork uses static props.
 
Back
Top