我想区分以下几种情况:
普通值类型(例如int)
可为空的值类型(例如int?)
引用类型(例如string)-可选,我不在乎是否将其映射到上面的(1)或(2)
我想出了以下代码,在情况(1)和(2)下工作正常:
static void Foo<T>(T a) where T : struct { } // 1
static void Foo<T>(T? a) where T : struct { } // 2
但是,如果我尝试检测这种情况(3),它将无法编译:
static void Foo<T>(T a) where T : class { } // 3
错误消息是类型'X'已经定义了具有相同参数类型的成员'Foo'。好吧,我无法以where T : struct和区别where T : class。
如果删除第三个函数(3),则以下代码也不会编译:
int x = 1;
int? y = 2;
string z = "a";
Foo (x); // OK, calls (1)
Foo (y); // OK, calls (2)
Foo (z); // error: the type 'string' must be a non-nullable value type ...
如何Foo(z)进行编译,将其映射到上述函数之一(或第三个具有另一个约束的函数,我没有想到)?
胡子哥哥
BIG阳
相关分类