Udostępnij przez


using (odwołanie w C#)

Słowo using kluczowe ma dwa główne zastosowania:

  • using definiuje zakres, na końcu którego obiekt jest usuwany:
    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);
    }
    
  • Dyrektywa using tworzy alias dla przestrzeni nazw lub typów importów zdefiniowanych w innych przestrzeniach nazw:
    using System;
    using System.IO;
    

Zobacz też