我需要使用多个using语句吗? [英] Do I need to use multiple using statements?

查看:78
本文介绍了我需要使用多个using语句吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于实用性考虑,这两个课程都是一次性的.

我了解using块的作用.但是我不确定它可以使用或需要使用的所有方式.

I understand what a using block does. But I'm not sure of all of the ways it can or needs to be used.

例如,这是正确的吗?

using (MyClass myClass = new MyClass(params))
{
     myClass.name = "Steve";

     SecondClass myClassSecond = new SecondClass(params);
     myClassSecond.name = "George";
     myClassSecond.msg = "Hello Man in the Yellow Hat";
}

以上两个类别都被处理掉了吗?

Are both classes above disposed of?

还是在using语句中都需要两个?

Or do I need both inside a using statement?

using (MyClass myClass = new MyClass(params))
{
     myClass.name = "Steve";

     using (SecondClass myClassSecond = new SecondClass(params))
     {
          myClassSecond.name = "George";
          myClassSecond.msg = "Hello Man in the Yellow Hat";
     }
}

以上是否正确,还是有更好的方法来使用多个using语句?

Is the above correct, or is there a better way to use multiple using statements?

推荐答案

  • 使用 using 语句,程序员可以指定使用资源的对象何时释放它们.
  • 提供给using语句的对象必须实现 IDisposable 接口.
  • 此接口提供了 Dispose 方法,该方法应释放对象的资源.
    • The using statement allows the programmer to specify when objects that use resources should release them.
    • The object provided to the using statement must implement the IDisposable interface.
    • This interface provides the Dispose method, which should release the object's resources.
    • 以下是显示using语句使用的示例:

      Here is the sample showing use of using statement:

      using System;
      //Object of this class will be given to using hence IDisposable
      class C : IDisposable        
      {
          public void UseLimitedResource()
          {
              Console.WriteLine("Using limited resource...");
          }
      
          //Dispose() Method
          void IDisposable.Dispose()
          {
              Console.WriteLine("Disposing limited resource.");
          }
      }
      
      class Program
      {
          static void Main()
          {
              using (C c = new C())  //Object of Class defined above
              {
                  c.UseLimitedResource();
                  //Object automatically disposed before closing.
              }                            
              Console.WriteLine("Now outside using statement.");
              Console.ReadLine();
          }
      }
      

      在以下情况下可以退出using语句:

      A using statement can be exited either when:

      • 达到 using 语句的末尾或
      • 如果引发异常并且控制权在语句结束之前离开语句块.

      哪种方法合适?

      正如你所说的

      出于实用性考虑,这两类都是一次性的

      Both classes for practicality sake are disposable

      .,那么您的第二种方法就是合适的方法.即:

      ., then your second approach is the appropriate one. that is:

      using (MyClass myClass = new MyClass(params))
      {
           myClass.name = "Steve";
      
           using (SecondClass myClassSecond = new SecondClass(params))
           {
                myClassSecond.name = "George";
                myClassSecond.msg = "Hello Man in the Yellow Hat";
           }
      }
      

      这篇关于我需要使用多个using语句吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆