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中return对象

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

 

相关文章:

  • 2021-07-30
  • 2022-12-23
  • 2022-03-06
  • 2022-12-23
  • 2022-12-23
  • 2021-12-04
  • 2022-12-23
猜你喜欢
  • 2021-10-16
  • 2022-01-31
  • 2022-02-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-15
  • 2022-12-23
相关资源
相似解决方案