Re: static constructor in derived class not being called
- From: "Octavio Hernandez" <dotnet@xxxxxxxxxxxx>
- Date: Mon, 8 Aug 2005 21:06:22 +0200
Elia,
Interesting stuff!
My guess is:
a) Before the call to MyDerived.DoSomething(), the CLR loads MyDerived and
immediately executes static constructor
b) Because of the inheritance relationship, the CLR loads MyBase, and
immediately initializes static field to 0 and then executes static
constructor (which in this case does nothing).
c) The call to DoSomething() proceeds, printing 0...
Regards - Octavio
"Elia Karagiannis" <ekaragiannis@xxxxxxxxxxxxx> escribió en el mensaje
news:uYJzqQEnFHA.2484@xxxxxxxxxxxxxxxxxxxxxxx
> The static constructor of a derived class never gets called if it has no
> other methods and the base class is only static
>
> I have the following base class and derived class.
>
> public class MyBase
> {
> protected MyBase()
> {}
>
> protected static int x;
>
> public static void DoSomething()
> {
> Console.WriteLine("X = " + x.ToString());
> }
> }
>
>
> // the derived class
> public class MyDerived : MyBase
> {
> static MyDerived()
> {
> x = 5;
> }
> }
>
> static void Main()
> {
> MyDerived.DoSomething(); // prints out zero instead of 5
> // the static
> constructor on the derived class never gets called
> // if I add a dummy
> static method to the derived class and call it then the
> // static
> constructor of the derived class will be called but I cant do this...
> }
>
>
> Any suggestions or can this just not be done??
>
.
- Follow-Ups:
- Re: static constructor in derived class not being called
- From: Octavio Hernandez
- Re: static constructor in derived class not being called
- References:
- static constructor in derived class not being called
- From: Elia Karagiannis
- static constructor in derived class not being called
- Prev by Date: Re: C# prototype of C: void**
- Next by Date: Re: what is the easiest way to allow .net component used by non dot net programs?
- Previous by thread: static constructor in derived class not being called
- Next by thread: Re: static constructor in derived class not being called
- Index(es):
Relevant Pages
|