小编use*_*166的帖子

重载泛型类的构造函数,基于类型

我试图重载默认构造函数之外的构造函数,只会调用int类型.我得到的最接近的是这个.

如果不可能,那为什么?

class Program
{
    static void Main()
    {
        //default construcotr get called
        var OGenerics_string = new Generics<string>();

        //how to make a different construcotr for type int
        var OGenerics_int = new Generics<int>();
    }

    class Generics<T>
    {
        public Generics()
        {
        }
        // create a constructor which will get called only for int
    }
}
Run Code Online (Sandbox Code Playgroud)

c# generics constructor

2
推荐指数
1
解决办法
228
查看次数

标签 统计

c# ×1

constructor ×1

generics ×1