Skip to content

ES CSharp Operator Lambda

Codex edited this page Oct 11, 2026 · 2 revisions

Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어

Expresiones lambda y expresiones corporales.

Manual C# · Guía Práctica

Declara una función anónima o un miembro con cuerpo de expresión concisa. Los parámetros y el tipo de resultado se infieren del contexto cuando es posible.

Ejemplo sencillo (Example.cs)

Guarde este ejemplo por separado como Example.cs. Ejecútelo independientemente de los demás ejemplos de esta página.

Example.cs:

// PROJECT-EXAMPLE
System.Func<int, int> twice = value => value * 2;
System.Console.WriteLine(twice(4));

Resultado:

8

Sintaxis

value => expression / (left, right) => { ... }

Ejemplo 1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

public static class Program
{
    public static async Task Main(string[] args)
    {
        Func<int, int> square = value => value * value;
        Console.WriteLine(square(4));
    }
}

Ejemplo 2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

public static class Program
{
    public static async Task Main(string[] args)
    {
        int offset = 3;
        Func<int, int> adjusted = value => { int result = value + offset; return result; };
        Console.WriteLine(adjusted(5));
    }
}

Fuente oficial: Microsoft Aprender.

Clone this wiki locally