Comparteix via


using (Referencia de C#)

La palabra clave using tiene dos usos principales:

  • La instrucción using define un ámbito al final del cual se elimina un objeto:
    string filePath = "example.txt";
    string textToWrite = "Hello, this is a test message!";
    
    // Use the using statement to ensure the StreamWriter is properly disposed of
    using (StreamWriter writer = new StreamWriter(filePath))
    {
        writer.WriteLine(textToWrite);
    }
    
  • La directiva using crea un alias para un espacio de nombres o importa tipos definidos en otros espacios de nombres:
    using System;
    using System.IO;
    

Consulte también