Created
December 23, 2014 10:48
-
-
Save Patrick84/593c9c92a49fa2526a37 to your computer and use it in GitHub Desktop.
Delegate-Examples => Methods: named + anonymous + lambda-expression
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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