Csharp/C Sharp/Language Basics/Using — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 11:39, 26 мая 2010

Demonstrate using statement

/*
C#: The Complete Reference 
by Herbert Schildt 
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/

// Demonstrate using statement. 
 
using System; 
using System.IO; 
 
public class UsingDemo { 
  public static void Main() { 
    StreamReader sr = new StreamReader("test.txt"); 
 
    // Use object inside using statement. 
    using(sr) { 
      Console.WriteLine(sr.ReadLine()); 
      sr.Close(); 
    } 
 
    // Create StreamReader inside the using statement. 
    using(StreamReader sr2 = new StreamReader("test.txt")) { 
      Console.WriteLine(sr2.ReadLine()); 
      sr2.Close(); 
    } 
  } 
}


This version does not include the using System statement

/*
C#: The Complete Reference 
by Herbert Schildt 
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// This version does not include the using System statement. 
 
public class ExampleConsoleWriteLine { 
 
  // A C# program begins with a call to Main(). 
  public static void Main() { 
 
    // Here, Console.WriteLine is fully qualified. 
    System.Console.WriteLine("A simple C# program."); 
  } 
}