Skip to content

FR CSharp Keyword using

Codex edited this page Oct 11, 2026 · 2 revisions

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

C#: using

Manuel C# · Guide pratique

Importe des noms, définit un alias ou gère une ressource IDisposable/IAsyncDisposable. L'importation elle-même n'ajoute pas la dépendance manquante.

Exemple simple (Example.cs)

Enregistrez cet exemple séparément dans Example.cs. Exécutez-le indépendamment des autres exemples de cette page.

Example.cs:

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

Résultat:

1

Syntaxe

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

la déclaration using est valable jusqu'à la fin de la portée. Pour .csx, utilisez using (...) { ... } si la déclaration de niveau supérieur est ambiguë pour la syntaxe du script.

Exemple 1 : formulaire de base

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

Exemple 2 : une autre forme pratique

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

Source officielle : Microsoft Apprendre.

Clone this wiki locally