Is it possible to use an anonymous delegate to return an object?
Something like so:
object b = delegate { return a; };
Is it possible to use an anonymous delegate to return an object?
Something like so:
object b = delegate { return a; };
Just declare somewhere these static functions:
public delegate object AnonymousDelegate();
public static object GetDelegateResult(AnonymousDelegate function)
{
return function.Invoke();
}
And anywhere use it as you want like this:
object item = GetDelegateResult(delegate { return "TEST"; });
or even like this
object item = ((AnonymousDelegate)delegate { return "TEST"; }).Invoke();
Yes, but only by invoking it:
And of course, the following is a lot simpler...
In the comments, cross-thread exceptions are mentioned; this can be fixed as follows:
If the delegate is the thing we want to run back on the UI thread from a BG thread:
Or the other way around (to run the delegate on a BG):