When would you use delegates in C

0 votes
In C#, how do you make use of delegates? Some use cases probably?
Jun 10 in C# by rajiv
• 1,620 points
21 views

1 answer to this question.

0 votes

Delegates come in handy for a variety of reasons.

They can be used for a variety of things, including filtering data sequences. In this case, you'd use a predicate delegate, which takes only one parameter and returns true or false based on the delegate's implementation.

Here's a stupid example from which you can probably derive something more useful:

using System;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<String> names = new List<String>
        {
            "Nicole Hare",
            "Michael Hare",
            "Joe Hare",
            "Sammy Hare",
            "George Washington",
        };

        // Here I am passing "inMyFamily" to the "Where" extension method
        // on my List<String>.  The C# compiler automatically creates 
        // a delegate instance for me.
        IEnumerable<String> myFamily = names.Where(inMyFamily);

        foreach (String name in myFamily)
            Console.WriteLine(name);
    }

    static Boolean inMyFamily(String name)
    {
        return name.EndsWith("Hare");
    }
}
answered Jun 11 by pranav
• 2,580 points

Related Questions In C#

0 votes
1 answer

When should you use C# indexers

First, let's talk about indexer. Class (or struct) ...READ MORE

answered Jun 23 in C# by krishna
• 2,540 points
18 views
0 votes
1 answer

What does the extension method do in c# and why do you need it

Extension Methods allow you to add additional ...READ MORE

answered Jun 21 in C# by krishna
• 2,540 points
19 views
0 votes
1 answer

What are the differences between C, C# and C++ in terms of real-world applications?

C is a bare-bones, straightforward, and clean ...READ MORE

answered May 30 in C# by rajiv
• 1,620 points
29 views
0 votes
1 answer
0 votes
1 answer

Pass Method as Parameter using C#

In .NET 3.5, you can utilize the ...READ MORE

answered Jun 14 in C# by krishna
• 2,540 points
19 views
0 votes
1 answer
0 votes
2 answers

Is there a .NET equivalent to Apache Hadoop?

Hadoop is a Java-based platform. So, to ...READ MORE

answered Jul 16, 2020 in Big Data Hadoop by Suhana
• 340 points
668 views
0 votes
1 answer

Validate String against USPS State Abbreviations

Try something like this: private static String states ...READ MORE

answered Sep 20, 2018 in IoT (Internet of Things) by Annie97
• 2,160 points
229 views
0 votes
1 answer

Examples of dynamic polymorphism in c#

Check the example of polymorphism below. We ...READ MORE

answered Jun 7 in C# by pranav
• 2,580 points
31 views
0 votes
1 answer

Print Pdf in C#

Using an installed Adobe Reader or any ...READ MORE

answered Jun 7 in C# by pranav
• 2,580 points
29 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP