Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#:Creating Multicast delegate with boolean return type

Tags:

c#

delegates

Hai Techies,

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

Thanks

like image 657
Shyju Avatar asked Aug 31 '09 08:08

Shyju


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

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));
}
like image 109
Lee Avatar answered Oct 13 '22 00:10

Lee


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;
    }
}
like image 38
KV Prajapati Avatar answered Oct 12 '22 23:10

KV Prajapati