Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

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 -

 

 

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.

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...