Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Static Code Blocks?

Status
Not open for further replies.

mhuggins

Programmer
Joined
Jan 22, 2005
Messages
3
Location
US
I have a static Hashtable variable in a class that I would like to populate prior to any instance of the class being created. However, it seems like the earliest point at which I can populate the Hashtable is within the constructor.

I know Java allows a static code block...

Code:
static {
   // code here
}

...which allows for static code to occur prior to any object being created from the class. Is something like this possible in C#?? If not, does anyone have any suggestions on how to work around this?
 
You can create a static constructor which will be called prior to the instance constructor.

The order in which things are (generally) built in a class is:

1. Static fields
2. Static constructor
3. Instance constructor

Read more at:

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Thanks so much, I really appreciate your help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top