When would you use delegates in C

0 votes
In C#, how do you make use of delegates? Some use cases probably?
Jun 10, 2022 in C# by rajiv
• 1,620 points
278 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, 2022 by pranav
• 2,590 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, 2022 in C# by krishna
• 2,820 points
249 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, 2022 in C# by krishna
• 2,820 points
397 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, 2022 in C# by rajiv
• 1,620 points
304 views
0 votes
1 answer

Is a Library a project or a C# source code file in Visual Studio?

A library project is a collection of ...READ MORE

answered Jun 7, 2022 in C# by rajiv
• 1,620 points
255 views
0 votes
1 answer

Pass Method as Parameter using C#

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

answered Jun 14, 2022 in C# by krishna
• 2,820 points
564 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
1,372 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
667 views
0 votes
1 answer

Examples of dynamic polymorphism in c#

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

answered Jun 7, 2022 in C# by pranav
• 2,590 points
584 views
0 votes
1 answer

Print Pdf in C#

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

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