S
Sunny
Hi all,
According C# Language Specification :
10.11 Static constructors:
The static constructor for a class executes at most once in a given
application domain. The execution of a static constructor is triggered
by the first of the following events to occur within an application
domain:
- An instance of the class is created.
- Any of the static members of the class are referenced.
Then can someone confirm that the following code does not act as it
should (seems like a bug), i.e. static constructor is NEVER called.
Pls, note that if you uncomment the first line in Main(), everything
runs as expected.
Also, my environment is VS.Net 2003/framework 1.1.
Thanks
Sunny
CODE (watch out for line wraps):
using System;
using System.Net;
using System.IO;
namespace TestApp
{
class TestApp
{
[STAThread]
static void Main(string[] args)
{
//MyChildClass.TheString = "Set";
MyChildClass.DoSomething();
Console.WriteLine("Press enter to continue");
Console.ReadLine();
}
}
public abstract class MyBaseClass
{
protected delegate void DoSomethingDelegate();
protected static DoSomethingDelegate DoSomethingFunc;
protected static string SomeString = "N/A";
public static void DoSomething()
{
Console.WriteLine("MyBase.DoSomething()");
DoSomethingFunc();
}
}
public class MyChildClass : MyBaseClass
{
static MyChildClass()
{
Console.WriteLine("static MyChildClass()");
DoSomethingFunc =
new DoSomethingDelegate(DoSomethingImpl);
}
protected static void DoSomethingImpl()
{
Console.WriteLine("DoSomething()");
}
public static string TheString
{
get{return SomeString;}
set{SomeString = value;}
}
}
}
According C# Language Specification :
10.11 Static constructors:
The static constructor for a class executes at most once in a given
application domain. The execution of a static constructor is triggered
by the first of the following events to occur within an application
domain:
- An instance of the class is created.
- Any of the static members of the class are referenced.
Then can someone confirm that the following code does not act as it
should (seems like a bug), i.e. static constructor is NEVER called.
Pls, note that if you uncomment the first line in Main(), everything
runs as expected.
Also, my environment is VS.Net 2003/framework 1.1.
Thanks
Sunny
CODE (watch out for line wraps):
using System;
using System.Net;
using System.IO;
namespace TestApp
{
class TestApp
{
[STAThread]
static void Main(string[] args)
{
//MyChildClass.TheString = "Set";
MyChildClass.DoSomething();
Console.WriteLine("Press enter to continue");
Console.ReadLine();
}
}
public abstract class MyBaseClass
{
protected delegate void DoSomethingDelegate();
protected static DoSomethingDelegate DoSomethingFunc;
protected static string SomeString = "N/A";
public static void DoSomething()
{
Console.WriteLine("MyBase.DoSomething()");
DoSomethingFunc();
}
}
public class MyChildClass : MyBaseClass
{
static MyChildClass()
{
Console.WriteLine("static MyChildClass()");
DoSomethingFunc =
new DoSomethingDelegate(DoSomethingImpl);
}
protected static void DoSomethingImpl()
{
Console.WriteLine("DoSomething()");
}
public static string TheString
{
get{return SomeString;}
set{SomeString = value;}
}
}
}