how to initialize static table?

  • Thread starter Thread starter Pawel Janik
  • Start date Start date
P

Pawel Janik

Hello..

I have some static functions, that do somple conversion. But to do this
conversion, I have to use some conversion tables. And now the question: how
to fill this conversion tables (the cannot be initialized in declaration,
this is too complex).

Now, i do somethink like this:

static void convert()
{
if(convtable is not filled) fill();
....do conversion)
}

but i don't want to check conversion table every time i do this
conversion....

Any better idea??
 
Pawel Janik said:
I have some static functions, that do somple conversion. But to do this
conversion, I have to use some conversion tables. And now the question: how
to fill this conversion tables (the cannot be initialized in declaration,
this is too complex).

Now, i do somethink like this:

static void convert()
{
if(convtable is not filled) fill();
...do conversion)
}

but i don't want to check conversion table every time i do this
conversion....

Any better idea??

Yup - use a static constructor, or just a static variable which has an
appropriate initializer. For instance:

static Table convtable = LoadConversionTable();

or

static Table convtable;

static MyClass
{
convtable = LoadConversionTable();
}
 
Pawel,
but i don't want to check conversion table every time i do this
conversion....

Any better idea??

Do it from a static constructor (aka type initializer). It runs only
once per type (per appdomain).

static YourClass() { ... }



Mattias
 
Mattias Sjögren said:
Do it from a static constructor (aka type initializer). It runs only
once per type (per appdomain).

Note that there is a subtle but occasionally important difference
between a type initializer and a static constructor. A static
constructor only exists when you've got:

static Foo()
{
....
}

whereas you get a type initializer when you do

static int x = 5;

There is a difference in terms of the flags the C# compiler puts on the
class, which can change the initialization timing. See
http://www.pobox.com/~skeet/csharp/beforefieldinit.html for more info.

(Almost certainly not relevant to the OP, but I thought you might be
interested Mattias :)
 
Jon said:
Note that there is a subtle but occasionally important difference
between a type initializer and a static constructor. A static
constructor only exists when you've got:

static Foo()
{
...
}

whereas you get a type initializer when you do

static int x = 5;

There is a difference in terms of the flags the C# compiler puts on
the class, which can change the initialization timing. See
http://www.pobox.com/~skeet/csharp/beforefieldinit.html for more info.

(Almost certainly not relevant to the OP, but I thought you might be
interested Mattias :)

Thanks to all !!
It works.

Pawel Janik
 
Note that there is a subtle but occasionally important difference

OK, fair enough, pardon my sloppy use of terminology.

But if you go strictly by those definitions, you could also argue that
RuntimeHelpers.RunClassConstructor() should be called
RuntimeHelpers.RunTypeInitializer().



Mattias
 
Mattias Sjögren said:
OK, fair enough, pardon my sloppy use of terminology.

No problem at all - I wasn't trying to have a dig at you :)
But if you go strictly by those definitions, you could also argue that
RuntimeHelpers.RunClassConstructor() should be called
RuntimeHelpers.RunTypeInitializer().

Possibly - I can't actually get that to do *anything* at the moment :(
I can't see how it could run just the class constructor, to be honest.

Type.TypeInitializer is definitely the whole type initializer though.
 
Back
Top