using 指令有两个用途: 
·创建命名空间的别名(using 别名)。 
·允许在命名空间中使用类型,例如,不必限定该命名空间中的类型使用(using 指令)。 
  using [alias = ]class_or_namespace;
其中: 
alias(可选) 
希望表示命名空间的用户定义符号。之后,您就可以使用 alias 表示命名空间名。 
class_or_namespace 
希望使用或为其提供别名的命名空间名,或希望为其提供别名的类名。
 
备注:
创建 using 别名以便更易于将标识符限定到命名空间或类。
创建 using 指令,以便在命名空间中使用类型而不必指定命名空间。using 指令不为您提供对可能嵌套在指定命名空间中的任何命名空间的访问。
命名空间分为两类:用户定义的命名空间和系统定义的命名空间。用户定义的命名空间是在代码中定义的命名空间。有关系统定义的命名空间的列表,请参见 .NET Framework 文档。
有关其他程序集中的引用方法的示例,请参见创建和使用 C# DLL。示例:
下面的示例显示了如何为命名空间定义和使用 using 别名:
// cs_using_directive.cs
using MyAlias = MyCompany.Proj.Nested;  // define an alias to represent a namespacenamespace MyCompany.Proj 
{
   public class MyClass 
   {
      public static void DoNothing() 
      {
      }
   }   namespace Nested   // a nested namespace
   {
      public class ClassInNestedNameSpace 
      {
         public static void SayHello() 
         {
            System.Console.WriteLine("Hello");
         }
      }
   }}public class UnNestedClass 
{
   public static void Main() 
   {
      MyAlias.ClassInNestedNameSpace.SayHello();   // using alias
   }
}
输出:
Hello示例:
下面的示例显示了如何为类定义 using 指令和 using 别名:
// cs_using_directive2.cs
using System;   // using directive
using AliasToMyClass = NameSpace1.MyClass;   // using alias for a classnamespace NameSpace1 
{
   public class MyClass 
   {
      public override string ToString() 
      {
         return "You are in NameSpace1.MyClass";
      }
   }
}namespace NameSpace2 
{
   class MyClass 
   {
   }
}namespace NameSpace3 
{
   using NameSpace1;   // using directive
   using NameSpace2;   // using directive   class Test 
   {
      public static void Main() 
      {
         AliasToMyClass somevar = new AliasToMyClass();
         Console.WriteLine(somevar);
      }
   }
}
输出:
You are in NameSpace1.MyClass

解决方案 »

  1.   

    using 语句:using 语句定义一个范围,在此范围的末尾将处理对象。
    using (expression | type identifier = initializer) statement
    其中: 
    expression 
    希望在退出 using 语句时调用 Dispose 的表达式。 
    type 
    identifier 的类型。 
    identifier 
    type 类型的名称或标识符。定义一个以上 type 类型的 identifier 是可以的。在每一个 identifier = initializer 的前边都有一个逗号。 
    initializer 
    创建对象的表达式。 
    statement 
    嵌入的语句或要执行的语句。 备注:
    在 using 语句中创建一个实例,确保退出 using 语句时在对象上调用 Dispose。当到达 using 语句的末尾,或者如果在语句结束之前引发异常并且控制离开语句块,都可以退出 using 语句。
    实例化的对象必须实现 System.IDisposable 接口。
    示例:
    // cs_using_statement.cs
    // compile with /reference:System.Drawing.dll
    using System.Drawing;
    class a
    {
       public static void Main()
       {
          using (Font MyFont = new Font("Arial", 10.0f), MyFont2 = new Font("Arial", 10.0f))
          {
             // use MyFont and MyFont2
          }   // compiler will call Dispose on MyFont and MyFont2      Font MyFont3 = new Font("Arial", 10.0f);
          using (MyFont3)
          {
             // use MyFont3
          }   // compiler will call Dispose on MyFont3   }
    }
      

  2.   

    详情请分别参考:ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/csref/html/vclrfusingdirective.htmms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/csref/html/vclrfusingstatement.htm
      

  3.   

    using 的两种主要用途
    1 将一个命名空间中的元素倾倒到另一个命名空间
       如: using System.Data.SqlClient;
    2 using 别名指令,将一个命名空间或其中的元素引入另外一个命名空间
       如:using [别名] = [命名空间的完全限定名].{元素名}
      

  4.   

    经常用的
    //读取文件
    private static void ReadFile()
    {
    using(FileStream fs = new FileStream("d:\\Service.Txt",FileMode.Open,FileAccess.Read))
    {
    StreamReader sr = new StreamReader(fs,Encoding.Default);
    Console.WriteLine(sr.ReadToEnd());
    sr.Close();
    }
    }//写文件
    private static void WriteFile()
    {
    using(FileStream fs = new FileStream("c:\\CreateText.txt",FileMode.Create,FileAccess.Write))
    {
    StreamWriter sr = new StreamWriter(fs);
    StringBuilder sb = new StringBuilder();
    for(int i=0;i<10000;i++)
    sb.Append(i.ToString("0000")+",");
    sr.Write(sb.ToString(0,sb.Length-1));
    sr.Close();
    }
    }