Skip to content

JA CSharp Operator Lambda

Codex edited this page Oct 11, 2026 · 2 revisions

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

ラムダ式とボディ式

ディレクトリ C# · 実践的な指導

無名関数または簡潔な式本体のメンバーを宣言します。パラメーターと結果の型は、可能な場合にはコンテキストから推測されます。

簡単な例 (Example.cs)

この例を Example.cs として別に保存し、このページの他の例とは独立して実行してください。

Example.cs:

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

結果:

8

構文

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

例 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));
    }
}

例 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));
    }
}

公式ソース: Microsoft Learn。

Clone this wiki locally