G
Guest
It seems the static constructor is supposed to be called, ONLY once for a
particular type. If that's the case, why does the following code throws an
exception?
using System;
using System.Collections.Generic;
using System.IO;
namespace GenericTypes
{
class Program
{
static void Main(string[] args)
{
MyList<int> lList;
try
{
lList = new MyList<int>();
}
catch
{
}
//Following line throws an error
//should the static constructor, be not called for this instance?
MyList<int> lList1 = new MyList<int>();
try
{
MyList<double> ldblList = new MyList<double>();
}
catch
{
}
}
class MyList<T>
{
static int i;
static List<T> _myList;
static MyList()
{
_myList = new List<T>();
System.Console.WriteLine("Called for " + typeof(T));
if (!typeof(T).IsEnum)
{
throw new ArgumentException("T must be an enum");
}
}
}
}
}
particular type. If that's the case, why does the following code throws an
exception?
using System;
using System.Collections.Generic;
using System.IO;
namespace GenericTypes
{
class Program
{
static void Main(string[] args)
{
MyList<int> lList;
try
{
lList = new MyList<int>();
}
catch
{
}
//Following line throws an error
//should the static constructor, be not called for this instance?
MyList<int> lList1 = new MyList<int>();
try
{
MyList<double> ldblList = new MyList<double>();
}
catch
{
}
}
class MyList<T>
{
static int i;
static List<T> _myList;
static MyList()
{
_myList = new List<T>();
System.Console.WriteLine("Called for " + typeof(T));
if (!typeof(T).IsEnum)
{
throw new ArgumentException("T must be an enum");
}
}
}
}
}