C# 裸类型约束
C# Naked type constraints
在"C# 6.0 in a Nutshell"中有一个裸类型约束用法的例子:
class Stack<T>
{
Stack<U> FilteredStack<U>() where U : T {...}
}
老实说,我不明白为什么要在这里使用这个约束条件。如果我将其删除并将 U 更改为 T,结果将相同。那有什么意义呢?
谢谢。
关键是 U
可以是 T
的子 class 的任何类型,而你得到的 Stack
是一堆 that 类型,而不是 T
。因此,添加到它的项目必须是 U
类型,并且如果它们不为空,则从中返回的项目保证是 U
。编译时类型检查的所有熟悉的喜怒哀乐。
Stack<T>
中的项目可以是 T
类型或 T
的任何子 class 类型。该方法的名称表明它正在返回一个堆栈,该堆栈仅包含父堆栈中实际上属于某个特定子class 的项目。一旦您保证新堆栈中的所有项目都属于更专门化的类型,那么如果新堆栈的类型也是这种类型,则更有用。
这是一个非常人为设计的例子(很明显,这个 "stack" class 实际上并没有做堆栈做的任何事情,但对于我们的例子来说它不需要):
public class A
{
public A(String s)
{
SA = s;
}
public String SA { get; set; }
}
public class B : A
{
public B(String s, string s1)
{
SA = s;
SB = s1;
}
public String SB { get; set; }
}
class Stack<T>
{
Stack<U> FilteredStack<U>() where U : T
{
return new Stack<U>(Items.OfType<U>());
}
public IEnumerable<T> Items { get { return _items; } }
public static void Test()
{
var s1 = new Stack<A>(new[] { new A("A1"), new B("B1", "Some other value") });
var s2 = s1.FilteredStack<B>();
// s2 is a strongly typed stack of type B
Console.WriteLine(s2.Items.First().SB);
}
private List<T> _items = new List<T>();
public Stack(IEnumerable<T> items) {
_items = new List<T>(items);
}
}
如果您有 Stack<Animal>
,您可以使用 FilteredStack<Dog>
获得 Stack<Dog>
。关键是您要确保传递给 FilteredStack
的 U
是从 T
派生的类型,但不一定是 T
泛型约束用于限制泛型类型、函数等的访问
其中 U : T 表示只有从 U 继承的 classes 才能访问 FilteredStack() 函数。那些未从 U 继承的 class 的对象不应该能够访问 FilteredStack() 并且它会给出编译错误。这就是类型安全的全部目的
在"C# 6.0 in a Nutshell"中有一个裸类型约束用法的例子:
class Stack<T>
{
Stack<U> FilteredStack<U>() where U : T {...}
}
老实说,我不明白为什么要在这里使用这个约束条件。如果我将其删除并将 U 更改为 T,结果将相同。那有什么意义呢?
谢谢。
关键是 U
可以是 T
的子 class 的任何类型,而你得到的 Stack
是一堆 that 类型,而不是 T
。因此,添加到它的项目必须是 U
类型,并且如果它们不为空,则从中返回的项目保证是 U
。编译时类型检查的所有熟悉的喜怒哀乐。
Stack<T>
中的项目可以是 T
类型或 T
的任何子 class 类型。该方法的名称表明它正在返回一个堆栈,该堆栈仅包含父堆栈中实际上属于某个特定子class 的项目。一旦您保证新堆栈中的所有项目都属于更专门化的类型,那么如果新堆栈的类型也是这种类型,则更有用。
这是一个非常人为设计的例子(很明显,这个 "stack" class 实际上并没有做堆栈做的任何事情,但对于我们的例子来说它不需要):
public class A
{
public A(String s)
{
SA = s;
}
public String SA { get; set; }
}
public class B : A
{
public B(String s, string s1)
{
SA = s;
SB = s1;
}
public String SB { get; set; }
}
class Stack<T>
{
Stack<U> FilteredStack<U>() where U : T
{
return new Stack<U>(Items.OfType<U>());
}
public IEnumerable<T> Items { get { return _items; } }
public static void Test()
{
var s1 = new Stack<A>(new[] { new A("A1"), new B("B1", "Some other value") });
var s2 = s1.FilteredStack<B>();
// s2 is a strongly typed stack of type B
Console.WriteLine(s2.Items.First().SB);
}
private List<T> _items = new List<T>();
public Stack(IEnumerable<T> items) {
_items = new List<T>(items);
}
}
如果您有 Stack<Animal>
,您可以使用 FilteredStack<Dog>
获得 Stack<Dog>
。关键是您要确保传递给 FilteredStack
的 U
是从 T
派生的类型,但不一定是 T
泛型约束用于限制泛型类型、函数等的访问
其中 U : T 表示只有从 U 继承的 classes 才能访问 FilteredStack() 函数。那些未从 U 继承的 class 的对象不应该能够访问 FilteredStack() 并且它会给出编译错误。这就是类型安全的全部目的