Action.cs
Исходник
using System;
using System.Windows.Forms;
public class TestLambdaExpression
{
public static void Main()
{
Action<string> messageTarget;
if (Environment.GetCommandLineArgs().Length > 1)
messageTarget = s => ShowWindowsMessage(s);
else
messageTarget = s => Console.WriteLine(s);
messageTarget("Hello, World!");
}
private static void ShowWindowsMessage(string message)
{
MessageBox.Show(message);
}
}
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 | } |
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 |