Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote:
If the values are primitive types, you can just declare the value in the
declaration of the static field.
If you require some more intricate initialization, then you can define a
static constructor, like this:
static()
{
// Do static initialization here.
}
Even if you require more intricate construction, you can call methods:
using System;
using System.Collections;
public class Test
{
static Hashtable map = CreateMap();
static void Main()
{
Console.WriteLine (map["first"]);
}
static Hashtable CreateMap()
{
Hashtable ret = new Hashtable();
ret["first"] = "hi";
ret["second"] = "there";
return ret;
}
}
Static constructors have a small performance penalty for frequent uses
of static methods (due to the lack of a beforefieldinit flag).
Strangely enough though, I tend to use static constructors despite
(just now) finding the above a neater way of doing things. Hmm.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog:
http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too