Skip to content

JA CSharp Keyword using

Codex edited this page Oct 11, 2026 · 2 revisions

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

C#: using

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

名前のインポート、エイリアスの設定、または IDisposable/IAsyncDisposable リソースの管理を行います。インポート自体は、不足している依存関係を追加しません。

簡単な例 (Example.cs)

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

Example.cs:

// PROJECT-EXAMPLE
using System;
using (var stream = new System.IO.MemoryStream())
{
    stream.WriteByte(65);
    Console.WriteLine(stream.Length);
}

結果:

1

構文

using Namespace; / using Alias = Type; / using var resource = ...; / await using ...;

using 宣言はスコープの終わりまで有効です。 .csx の場合、スクリプト構文に対してトップレベルの宣言があいまいな場合は、using (...) { ... } を使用します。

例 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)
    {
        using var stream = new MemoryStream();
        stream.WriteByte(65);
        Console.WriteLine(stream.Length);
    }
}

例 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)
    {
        await using var stream = new MemoryStream();
        await stream.WriteAsync(new byte[] { 1, 2, 3 });
        Console.WriteLine(stream.Length);
    }
}

公式ソース: Microsoft Learn。

Clone this wiki locally