Skip to content

Instantly share code, notes, and snippets.

@Patrick84
Created December 23, 2014 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Patrick84/593c9c92a49fa2526a37 to your computer and use it in GitHub Desktop.
Save Patrick84/593c9c92a49fa2526a37 to your computer and use it in GitHub Desktop.
Delegate-Examples => Methods: named + anonymous + lambda-expression
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SandboxConsole
{
// Declare delegate -- defines required signature:
delegate double DTaschenrechner(double zahl);
class Program
{
// Define: Regular method that matches the signature of the delegate:
static double MalZwei(double input)
{
return (input * 2);
}
static void Main(string[] args)
{
// Instantiate delegate with named method:
DTaschenrechner rechner = MalZwei;
Console.WriteLine( rechner(5) ); // Ausgabe: 10
// Instantiate delegate with anonymous method:
DTaschenrechner rechner2 = delegate(double input)
{
return (input * 2);
};
Console.WriteLine( rechner2(5) ); // Ausgabe: 10
// Instatiate delegate with lambda expression
DTaschenrechner rechner3 = s => s * 2;
double zahl = rechner3(5);
Console.WriteLine(zahl); // Ausgabe: 10
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment