Skip to content

ES CSharp Keyword using

Codex edited this page Oct 11, 2026 · 2 revisions

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

C#: using

Manual C# · Guía Práctica

Importa nombres, establece un alias o administra un recurso IDisposable/IAsyncDisposable. La importación en sí no agrega la dependencia que falta.

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

Resultado:

1

Sintaxis

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

la declaración de uso es válida hasta el final del alcance. Para .csx use (...) { ... } si la declaración de nivel superior es ambigua para la sintaxis del script.

Ejemplo 1: forma básica

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

Ejemplo 2: otra forma práctica

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

Fuente oficial: Microsoft Aprender.

Clone this wiki locally