Last active 1704332767

Example of an Action/Func in C#

DraxCodes's Avatar DraxCodes revised this gist 1704332767. Go to revision

No changes

DraxCodes's Avatar DraxCodes revised this gist 1704332587. Go to revision

1 file changed, 11 insertions

Func.cs(file created)

@@ -0,0 +1,11 @@
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

DraxCodes's Avatar DraxCodes revised this gist 1704332462. Go to revision

1 file changed, 22 insertions

Action.cs(file created)

@@ -0,0 +1,22 @@
1 + using System;
2 + using System.Windows.Forms;
3 +
4 + public 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 + }
Newer Older