问题描述
在C#中,我们如何定义它接受一个DateTime对象,并返回一个布尔多播委托。
感谢
公共委托布尔美孚(DateTime的时间戳);
这是如何声明和你描述签名的委托。所有代表都是潜在的组播,他们只是需要初始化。如:
公共BOOL IsGreaterThanNow(DateTime的时间戳)
{
返回DateTime.Now<时间戳;
}
公共BOOL IsLessThanNow(DateTime的时间戳)
{
返回DateTime.Now>时间戳;
}
美孚F1 = IsGreaterThanNow;
美孚F2 = IsLessThanNow;
美孚下跌= F1 + F2;
调用秋天
,在这种情况下会打电话无论 IsGreaterThanNow()
和 IsLessThanNow()
。
这是什么不就是给你访问的每个返回值。你得到的是返回的最后一个值。如果要检索每一个值,你必须手动处理组播像这样:
列表<布尔> returnValues =新的List<布尔>();
的foreach(富f由于fAll.GetInvocationList())
{
returnValues.Add(F(时间戳));
}
in C#, how can we define the multicast delegate which accepts a DateTime object and return a boolean.
Thanks
public delegate bool Foo(DateTime timestamp);
This is how to declare a delegate with the signature you describe. All delegates are potentially multicast, they simply require initialization. Such as:
public bool IsGreaterThanNow(DateTime timestamp)
{
return DateTime.Now < timestamp;
}
public bool IsLessThanNow(DateTime timestamp)
{
return DateTime.Now > timestamp;
}
Foo f1 = IsGreaterThanNow;
Foo f2 = IsLessThanNow;
Foo fAll = f1 + f2;
Calling fAll
, in this case would call both IsGreaterThanNow()
and IsLessThanNow()
.
What this doesn't do is give you access to each return value. All you get is the last value returned. If you want to retrieve each and every value, you'll have to handle the multicasting manually like so:
List<bool> returnValues = new List<bool>();
foreach(Foo f in fAll.GetInvocationList())
{
returnValues.Add(f(timestamp));
}
这篇关于C#:创建具有布尔返回类型多播委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!