泛型方法
为什么我下面的代码会出错?是不是因为U没有约束 范围大比where T:B要大,所以不能用A<U> a = new A<U>(); 编译器就不通过了?
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace delete2
{
class A<T> where T : B
{
public void liweike<U>()
{ A<U> a = new A<U>(); } //
}
class B
{ }
class C : B
{ }
class Program
{
static void Main(string[] args)
{
}
}
}
如果改成这样就通过了:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace delete2
{
class A<T>
{
public void liweike<U>()
{ A<U> a = new A<U>(); } //
}
class B
{ }
class C : B
{ }
class Program
{
static void Main(string[] args)
{
}
}
}