• 00
  • :
  • 00
  • :
  • 00

Using中return对象

 class Program
    {
        static void Main(string[] args)
        {
            Test test = new Test();
            var a = test.CreateA();
            test.SayHello(a);

            Console.ReadKey();
        }


    }

    public class Test
    {
        public A CreateA()
        {
            using (A a = new A())
            {
                return a;
            }
        }
        public void SayHello(A a)
        {
            a.Hello();
        }
    }



    public class A : IDisposable
    {
        public void Dispose()
        {
            Console.WriteLine("Dispose被调用");
        }
        public void Hello()
        {
            Console.WriteLine("Hello A");
        }
    }

结论:如果对象需要返回,则不能在using中创建

 

posted @ 2019-02-26 15:42  Garson_Zhang  阅读(376)  评论(0编辑  收藏  举报