Giter Club home page Giter Club logo

delegates-in-csharp's Introduction

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

delegates-in-csharp's People

Contributors

liteobject avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.