[c#] 람다식을 사용하여 간단한 예제를 보여주세요.
using System;
namespace LambdaExpressionExample
{
class Program
{
static void Main(string[] args)
{
// Using lambda expression to define a simple function
Func<int, int, int> add = (a, b) => a + b;
// Calling the function using the lambda expression
int result = add(3, 5);
Console.WriteLine("Result of addition: " + result);
}
}
}
In this example, we used a lambda expression (a, b) => a + b
to define a simple addition function and assigned it to a Func<int, int, int>
delegate. We then called the function with arguments 3
and 5
and printed the result.