Can you show me cases that i can use delegate on it by code ?

ahmed salah 3,216 Reputation points
2022-07-19T22:15:38.73+00:00

I work on c# i'm not familiar on delegate and not use on it before

so please can you help me

by showing code for every case from that below

after reading i summarize that delegate used for cases below :

Delegates are used in the following cases:

1- Delegates can be used to handle(call/invoke) multiple methods on a single event.
2- Delegates can be used to define callback(asynchronous) methods.
3- Delegates can be used for decoupling and implementing generic behaviors.
4- Delegates can be invoked method at runtime.
5- Delegates can be used in LINQ for parsing the ExpressionTree.

can you show me implementation for every cases please

meaning show to me sample for every case by console implement

static main()  
{  
}  
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,346 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,203 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,117 questions
{count} vote

2 answers

Sort by: Most helpful
  1. Reza Aghaei 4,936 Reputation points MVP
    2022-07-20T01:31:31.113+00:00

    Example 1 - Multiple event handlers for an event

    You can assign multiple event handler to an event.

    The following example shows how you can assign multiple event handlers to an event:

    using System;  
    namespace ConsoleApp1  
    {  
        class Program  
        {  
            static void Main(string[] args)  
            {  
                var timer = new System.Timers.Timer();  
                timer.Elapsed += Timer_Elapsed1;  
                timer.Elapsed += Timer_Elapsed2;  
                timer.Interval = 1000;  
                timer.Start();  
                Console.ReadLine();  
                timer.Stop();  
                timer.Dispose();  
            }  
            private static void Timer_Elapsed1(object sender, System.Timers.ElapsedEventArgs e)  
            {  
                Console.WriteLine("Elapsed 1");  
            }  
            private static void Timer_Elapsed2(object sender, System.Timers.ElapsedEventArgs e)  
            {  
                Console.WriteLine("Elapsed 2");  
            }  
        }  
    }  
    

    You can use either of the following syntaxes for the event handlers:

     timer.Elapsed += Timer_Elapsed1  
    

    Or

     timer.Elapsed += new ElapsedEventHandler(Timer_Elapsed1)  
    

    Where Timer_Elapsed1 is a method like this:

    private static void Timer_Elapsed1(object sender, System.Timers.ElapsedEventArgs e)  
    {  
        /* code */   
    }  
    

    Or

    timer.Elapsed += delegate (object sender, System.Timers.ElapsedEventArgs e) {  
        /* code */   
    };  
    

    Or

    imer.Elapsed += (sender, e) => {   
        /* code */  
    };  
    

    To learn more about event handling take a look at:

    Example 2 - Using AsyncCallback in APM

    The following example shows how you can use an AsyncCallBack in Asynchronous Programming Model (APM) pattern. It basically is passing a delegate to a method as a parameter:

    using System;  
    using System.IO;  
      
    namespace ConsoleApp1  
    {  
        class Program  
        {  
            static Stream fs;  
            static void Main(string[] args)  
            {  
                var file = new System.IO.FileInfo(@"c:\temp\Clean-CMClientCache.log");  
                var buffer = new byte[file.Length];  
                fs = new FileStream(file.FullName, FileMode.Open, FileAccess.Read,  
                    FileShare.Read, 500, FileOptions.Asynchronous);  
                Console.WriteLine("Loading file ...");  
      
                fs.BeginRead(buffer, 0, buffer.Length, MyAsyncCallBack, null);  
                Console.ReadLine();  
            }  
            private static void MyAsyncCallBack(IAsyncResult ar)  
            {  
                Console.WriteLine("Load file completed.");  
                var bytes = fs.EndRead(ar);  
                Console.WriteLine($"The file is {bytes}  Bytes");  
                fs.Close();  
                fs.Dispose();  
            }  
        }  
    }  
    

    You can learn more:

    Example 3 - Decoupling and Generic behavior

    You can use delegates to inject different behavior, and decouple your classes/methods from implementation details. The same way that you can inject an interface in a class constructor, you can inject a delegate. It basically means you can pass any method which can implement the signature of the delegate, which means you can inject different behavior based on the same contract.

    Every time that you are passing a delegate to a method, property or constructor, you are doing this; for example all IEnumerable extension methods are doing this.

    In the following example, I've implemented a Filter method which accepts a delegate as a condition; so I can filter a list, based on any condition; The condition could be any method which accepts an element and just return true, if it should be included in the result:

    using System;  
    using System.Collections.Generic;  
      
    namespace ConsoleApp1  
    {  
        delegate bool Condition<T>(T input);  
        static class Extensions  
        {  
            public static IEnumerable<T> Filter<T>(this IEnumerable<T> input, Condition<T> condition)  
            {  
                foreach (var item in input)  
                {  
                    if (condition(item))  
                        yield return item;  
                }  
            }  
        }  
        class Program  
        {  
            static void Main(string[] args)  
            {  
                var numbers = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };  
                Console.WriteLine("Even numbers:");  
                var even = numbers.Filter(delegate (int x) { return x % 2 == 0; });  
                // Or simply:   
                //var even = numbers.Filter(x => x % 2 == 0);  
                foreach (var item in even)  
                {  
                    Console.WriteLine(item);  
                }  
                Console.WriteLine("Odd numbers:");  
                var odd = numbers.Filter(delegate (int x) { return x % 2 == 1; });  
                foreach (var item in odd)  
                {  
                    Console.WriteLine(item);  
                }  
                Console.ReadLine();  
            }  
        }  
    }  
    

    Example 4 - Invoke delegate

    Delegate is a type which encapsulates a method; it's something like method a pointer and you can invoke the delegates the same way that you invoke methods, assuming you have a method having the signature void Method1() in a delegate variable, like m, then you can invoke it simply like: m();.

    Here is an example, where we have created a delegate to say hello in a language, and provided two implementations-basically two methods, and later invoked the two delegates:

    using System;  
      
    namespace ConsoleApp1  
    {  
        delegate string Hi(string name);  
        class Program  
        {  
            static string HiInEnglish(string name) { return $"Hi {name}!"; }  
            static string HiInDanish(string name) { return $"Hej {name}!"; }  
      
            static void Main(string[] args)  
            {  
                var hi1 = new Hi(HiInEnglish);  
                var hi2 = new Hi(HiInDanish);  
      
                var str1 = hi1("John");  
                var str2 = hi2("John");  
      
                Console.WriteLine(str1);  
                Console.WriteLine(str2);  
      
                Console.ReadLine();  
            }  
        }  
    }  
    

    Example 5 - Delegates in Linq

    Delegates are heavily used in Linq extension methods in Enumerable class. Look at the following example:

    using System;  
    using System.Linq;  
      
    namespace ConsoleApp1  
    {  
        class Program  
        {  
            static void Main(string[] args)  
            {  
                var numbers = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };  
                var odd = numbers.Where(x => x % 2 == 1);  
                odd.ToList().ForEach(x => { Console.WriteLine(x); });  
                Console.ReadLine();  
            }  
        }  
    }  
    

    In above example, x => x % 2 == 1 is an Expression<TDelegate> and x => { Console.WriteLine(x); } is an Action<T>.


  2. Karen Payne MVP 35,031 Reputation points
    2022-07-26T20:43:27.727+00:00

    Here are a couple examples (full source links point to classes in projects)

    • Iterating data using EF Core public delegate void OnIteratePersonGrades(PersonGrades personData); full source
    • An example used for building a on-demand WHERE IN clause public delegate void CommandText(string sender) full source
    • Example for working with System.Threading.Timer full source
    • Several examples using TaskDialog full source
    • Example for handling unhandled exceptions full source
    0 comments No comments