Delegates in C# and Types and its usage:

Image
In C#, delegates are a type that represents references to methods. They are used to define and work with method signatures dynamically. Here are some types of delegates and related concepts in C#: Types of Delegates: Singlecast Delegates: ✅️Singlecast delegates refer to delegates that can hold references to only one method at a time. They are declared using the delegate keyword and can be used for scenarios where a single method needs to be invoked. delegate void MyDelegate(int x); SingleCast Delegate useful to perform Calculation logic based on Methods.Here is an simple example delegate int AddDelegate(int a, int b); static void SingleCastDelegates() { // Define the delegate AddDelegate addDelegate = (a, b) => a + b; // Use the delegate to perform addition int result = addDelegate(5, 10); Console.WriteLine(result); // Output: 15 } Multicast Delegates: ✅️Multicast delegates can hold references to ...

C#- Delegates vs Events

Image
Delegates and events are closely related concepts in C# and .NET, and they are often used together to implement the Observer design pattern. Here are the key differences between delegates and events, along with their common usage: They allow you to select and transform data from the source collection or query into a new form. Here are some common projection operators Delegates: A delegate is a type that represents references to methods with a specific signature. It acts as a function pointer, allowing you to encapsulate a method and pass it around in your code. Usage: Delegates are used to define and work with method signatures. They are commonly used in scenarios where a callback mechanism is needed, such as in event handling or asynchronous programming. public delegate void MyDelegate(string message); public class MyClass { public void MyMethod(string message) { Console.WriteLine...

What is Generic Delegates and its Types.

In C#, a generic delegate is a delegate that can work with any data type. This is achieved by using the Func or Action delegates along with generics. Here's an example of a generic delegate using Func: using System; class Program { static void Main() { Func add = (a, b) => a + b; Func concatenate = (a, b) => a + b; Console.WriteLine(add(1, 2)); // Outputs: 3 Console.WriteLine(concatenate("Hello, ", "world!")); } } In this example, Func int, int, int is a generic delegate that takes two int parameters and returns an int. Similarly, Func string, string, string takes two string parameters and returns a string. Action Delegates: 🔸Action delegates are used for methods that do not return a value (void methods). They can take up to 16 input parameters...

Use Delegate in Callback

You want to be notified when the download is complete so that you can perform some action, such as displaying a message to the user. You can use a callback delegate to achieve this: using System; using System.Net; using System.Threading.Tasks; class FileDownloader { // Define a delegate for the callback public delegate void DownloadCompletedCallback(string message); // Method to download a file asynchronously public async Task DownloadFileAsync(string url, DownloadCompletedCallback callback) { using (var client = new WebClient()) { try { // Download the file await client.DownloadFileTaskAsync(url, "downloadedFile.txt"); // Invoke the callback to notify that the download is complete callback("Download completed successfully"); } catch (Exception ex) { // Invoke the callback wit...