Demonstrate using statement : Using « Language Basics « C# / C Sharp






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

           
       








Related examples in the same category

1.This version does not include the using System statementThis version does not include the using System statement