Skip to content

LiteObject/delegates-in-csharp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Action and Func delegates in C#

If you're already familier with the concept of delegate, then you can skip the next section titled "What is a delegate?."

What is a delegate?

In C#, a delegate is a way to encapsulate a method reference that allows you to treat the method as an objects. Let's take a look at an example here:

// Declare your delegate
public delegate void MyDelegate(string message);

static void Main(string[] args)
{
  // Instantiate the delegate by assigning the `PrintMessage` method
  MyDelegate myDelegate = PrintMessage;

  // Call the delegate with the message "Hello, world!".
  myDelegate("Hello, world!");
}

static void PrintMessage(string message)
{
  Console.WriteLine(message);
} 

In .NET there are two commonly used generic delegate types:

  • Action
  • Func

What is an Action Delegate?

The Action delegate is a generic type delegate that represents a method that does not return a value. It can take up to 16 input parameters of any type. Here are a few examples:

// Action with no parameters
Action print1 = () => Console.WriteLine("Hello World!");
print1(); // Output: Hello World!

// Action with one parameter
Action<string> print2 = (message) => Console.WriteLine(message);
print2("Hello World!"); // Output: Hello World!

// Action with multiple parameters
Action<string,string> print3 = (message1, message2) => Console.WriteLine($"{message1} {message2}");
print3("Hello", "World!"); // Output: Hello World!

In the first example, we define an Actoion delegate named print1 that takes no parameters and simply writes "Hello World!" to the console when invoked. We can then call print1() to execute the method.

In the second example, we define an Action delegate named print2 that takes a string (message) as a parameter. When we invoke it, it prints the message to the console.

In the third example, we define an Action delegate named print3 that takes two string messages as parameters. When we invoke it, it prints those messages in sequence.

What is a Func Delegate?

The Func delegate is a generic type delegate that represents a method that takes input parameters and returns a value. The last type argument of Func represents the return type. It can take up to 16 input parameters.

Links

About

Action and Func delegates in C#

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages