代表,是的我想了解这个概念的使用,它有助于在C#中解决哪些问题。到目前为止,我非常喜欢它提供代码的Decoupling工具。然后我遇到了谓语代表。根据this MSDN library article it shows they are, delegates that takes any data type paramter and returns a bool type value...。只能将谓词委托调用为布尔委托?
它还说,这个特别代表采取仿制药,<T>
,能说那么任何类型...
因此,它是正确的说谓词委托仅仅是一个布尔代表?这意味着任何具有布尔返回类型的委托?还是有更多的指定与不同的名称:谓词委托..?
E.g.
delegate bool BooleanDelegate(anytype parameter);
BooleanDelegate bd = new BooleanDelegate(yesno);
//assuming parameter type is int
MessageBox.Show(bd.Invoke(2).ToString());
public bool yesno(anytype parameter)
{
If (parameter == 2)
{
return true;
}
Else
{
return false;
}
}
谢谢,抱歉花了我的时间来体验,并在同一行上有更多questiosn :)你说***参数不能是ref或out。***,所以它必须是'param'? – aspiring 2013-02-28 03:36:59
当我们查看Predicate <>的[signature](http://msdn.microsoft.com/en-us/library/bfcke1bz.aspx)时,我们发现参数'T obj'确实不会携带'params '修饰者。它不能,因为我们不知道'T'是否是一个'SomeType []'数组。当委托类型签名不具有“params”时,如果方法具有“params”,则方法必须以未扩展的形式应用。例如。如果方法是静态布尔Meth(params string [] args){...}',那么你可以说'Predicate p1 = Meth;',但是你不能扩展,所以它不能用'Predicate p2 = Meth;'。 –
2013-02-28 06:16:27