How to declare a generic delegate with an out parameter [duplicate]

懵懂的女人 提交于 2019-11-28 09:39:56
Andrew Bezzub

Actually, Func is just a simple delegate declared in the .NET Framework. Actually, there are several Func delegates declared there:

delegate TResult Func<TResult>()
delegate TResult Func<T, TResult>(T obj)
delegate TResult Func<T1, T2, TResult>(T1 obj1, T2 obj2)
delegate TResult Func<T1, T2, T3, TResult>(T1 obj1, T2 obj2, T3 obj3)
delegate TResult Func<T1, T2, T3, T4, TResult>(T1 obj1, T2 obj2, T3 obj3, T4 obj4)
delegate TResult Func<T1, T2, ... , T16, TResult>(T1 obj1, T2 obj2, ..., T16 obj16)

So the only thing you can do is declare your custom delegate:

delegate bool MyFunc<T1, T2>(T1 a, out T2 b)

You need to make your own delegate type, like this:

delegate bool MyFunc(Type1 a, out Type2 b);

You might want to rethink your design. Do you really need to complicate your code by adding an out parameter?

You can wrap the bool return type and the second out type in their own class (or .NET 4.0 Tuple) and use that as a return type:

public Func<Type1, Tuple<Type2, bool>> DetectMethod;

Of course when you want to use the delegates to reference try-parse methods, you are on the right track and you'll need to define a new delegate as others already described.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!