本文介绍了将使用空上的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 将下面的代码工作,如果资源没有实现IDisposable T =资源的新T()?; 使用(资源作为IDisposable接口) {:} 解决方案 是的,它会工作,检查此运行测试: [TestMethod的] 公共无效TestMethod8() {使用(新MyClass的()为IDisposable接口) {} } 公共类MyClass的{} 这只是运行没有任何问题。如果相应的类实现IDisposable它会调用它,如果不是它仍然可以工作/运行。) 更新:正如其他人所说我也想知道什么是你想给它的使用。我的猜测是您有类似的工厂可以从不同的类,其可以是或可以不是一次性的实例:。) Will the following code work if resource doesn't implement IDisposable? T resource = new T(); using (resource as IDisposable) { ... } 解决方案 Yes, it will work, check this running test:[TestMethod]public void TestMethod8(){ using (new MyClass() as IDisposable) { }}public class MyClass { }It just runs without any issue. If the corresponding class implements IDisposable it will call it, if not it will still work/run :).Update: As others have said, I also wonder what's the use you want to give to it. My guess is you have something like a factory that can get instances from different classes, which may or may not be disposable :). 这篇关于将使用空上的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-27 12:39