Dernière activité 1704332767

Example of an Action/Func in C#

Action.cs Brut
1using System;
2using System.Windows.Forms;
3
4public class TestLambdaExpression
5{
6 public static void Main()
7 {
8 Action<string> messageTarget;
9
10 if (Environment.GetCommandLineArgs().Length > 1)
11 messageTarget = s => ShowWindowsMessage(s);
12 else
13 messageTarget = s => Console.WriteLine(s);
14
15 messageTarget("Hello, World!");
16 }
17
18 private static void ShowWindowsMessage(string message)
19 {
20 MessageBox.Show(message);
21 }
22}
Func.cs Brut
1 Func<string, string> convert = delegate(string s)
2 {
3 return s.ToUpper();
4 };
5
6 string name = "Dakota";
7 Console.WriteLine(convert(name));
8
9// This code example produces the following output:
10//
11// DAKOTA