C#:Creating Multicast delegate with boolean return type
Asked Answered
N

5

12

Hai Techies,

in C#, how can we define the multicast delegate which accepts a DateTime object and return a boolean.

Thanks

Nigger answered 31/8, 2009 at 8:43 Comment(0)
B
37
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));
}
Bernadine answered 31/8, 2009 at 8:52 Comment(0)
A
3
class Test
{
    public delegate bool Sample(DateTime dt);
    static void Main()
    {
        Sample j = A;
        j += B;
        j(DateTime.Now);

    }
    static bool A(DateTime d)
    {
        Console.WriteLine(d);
        return true;
    }
    static bool B(DateTime d)
    {
        Console.WriteLine(d);
        return true;
    }
}
Aphrodisia answered 31/8, 2009 at 8:49 Comment(0)
M
3

Any delegate can be a multicast delegate

delegate bool myDel(DateTime s);
myDel s = someFunc;
s += someOtherFunc;

msdn

A useful property of delegate objects is that they can be assigned to one delegate instance to be multicast using the + operator. A composed delegate calls the two delegates it was composed from. Only delegates of the same type can be composed.

EDIT: A delagate has a method GetInvocationList which returns a list with the attached methods.

Here is a reference about Delegate invocation

foreach(myDel d in s.GetInvocationList())
{
   d();
}
Moonier answered 31/8, 2009 at 8:50 Comment(1)
What happens to the return values of a composed delegate when you invoke it? Is there some sort of array that allows you to inspect the return values after they are called? I've used multicast delegates before, but never thought about them having a return type.Leela
E
0

I was stumbling upon the same problem. I searched and found this in msdn.

http://msdn.microsoft.com/en-us/library/2e08f6yc(v=VS.100).aspx

There are two methods on delegates

  • BeginInvoke
  • EndInvoke

The link describes these in detail, with code samples.

We can hook into these methods to handle the return values of the delegates.

Eolian answered 10/2, 2014 at 14:50 Comment(0)
A
0

In your case , Instead of creating a delegate yourself ,

it is better to use pre-defined delegates in C# such as Func and Predicate :

public delegate TResult Func<in T1, in T2, out TResult>(T1 arg1, T2 arg2);

and

public delegate bool Predicate<in T>(T obj);
Auk answered 15/12, 2018 at 13:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.