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中创建
慎于行,敏于思!GGGGGG