using System;
namespace ConsoleApplication15
{
using System.Collections.Generic;
using System.Threading;
public static class Program
{
static void Main(string[] args)
{
var test1 = new Test<List<int>>();
var t = new Thread(Tester);
t.Start();
var test2 = new Test<List<int>>();
var test3 = new Test<List<int>>();
var test4 = new Test<List<int>>();
var test5 = new Test<List<int>>();
test1.Do();
test2.Do();
test3.Do();
test4.Do();
test5.Do();
}
private static void Tester()
{
var test5 = new Test<IList<int>>();
test5.Do();
}
}
public class Test<T> where T : IEnumerable<int>
{
private static Something something;
static Test()
{
Console.WriteLine("IM static created ");
something = new Something();
Console.WriteLine(something.ToString());
}
public Test()
{
Console.WriteLine("IM created ");
}
public void Do()
{
Console.WriteLine("Do something! ");
}
}
public class Something
{
public Something()
{
Console.WriteLine("Create something");
}
}
}
当我运行上述代码时,我期望在
static Test()
中的静态构造函数只会被调用一次,但是当我运行代码时,静态构造函数被调用了两次!!!当我删除这行代码
<T> where T : IEnumerable<int>
后,一切正常(静态构造函数只被调用一次)?!!第一次调用的断点如下所示:
![This is the breakpoint for the first call](https://istack.dev59.com/oIqKw.webp)
![This is the breakpoint for the second call](https://istack.dev59.com/Aevy6.webp)
Test<List<int>>
不等于Test<IList<int>>
- Sorax