Skip to content

Commit 0aea368

Browse files
committed
4.13.3.2: Code Example for Action
1 parent cb01575 commit 0aea368

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

action.cs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
3+
class ActionDemo
4+
{
5+
static void Main(string[] args)
6+
{
7+
// 1. Using a named method
8+
Action namedMethodAction = NamedMethod;
9+
namedMethodAction();
10+
11+
// 2. Using an anonymous method
12+
Action anonymousMethodAction = delegate
13+
{
14+
Console.WriteLine("Anonymous Method Action");
15+
};
16+
anonymousMethodAction();
17+
18+
// 3. Using a lambda expression
19+
Action lambdaAction = () => Console.WriteLine("Lambda Action");
20+
lambdaAction();
21+
22+
// 4. Using Action with parameters
23+
Action<int, string> actionWithParams = (num, text) =>
24+
Console.WriteLine($"Number: {num}, Text: {text}");
25+
actionWithParams(10, "Hello");
26+
}
27+
28+
// named method used for first Action instance on line 10
29+
static void NamedMethod()
30+
{
31+
Console.WriteLine("Named Method Action");
32+
}
33+
}

action.csproj

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
6+
</PropertyGroup>
7+
<ItemGroup>
8+
<Compile Include="action.cs" />
9+
</ItemGroup>
10+
</Project>

0 commit comments

Comments
 (0)