假设我有一个一次性对象MyDisposable,它将另一个一次性对象作为构造函数参数.
using(MyDisposable myDisposable= new MyDisposable(new AnotherDisposable()))
{
//whatever
}
Run Code Online (Sandbox Code Playgroud)
假设myDisposable不处理它AnotherDisposable内部的处理方法.
这只能正确处理myDisposable吗?还是处理它AnotherDisposable?
我有以下课程:
private static readonly string ConnectionString = "Dummy";
public static SqlConnection GetConnection()
{
SqlConnection Connection = new SqlConnection(ConnectionString);
return Connection;
}
public static SqlDataAdapter GetDataAdapter(string Query)
{
SqlDataAdapter Adapt = new SqlDataAdapter(Query, GetConnection());
return Adapt;
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助.
当创建一个实现一个元素IDisposable,Dispose()是在结束所谓的using块还,如果抛出一个异常,如果我是正确的.
但是,在ClassB一次性元素的构造函数中创建新元素时,如果IDisposable实现了ClassB的对象,是否也会被处置?
using (ClassA a = new ClassA(new ClassB()))
{
}
Run Code Online (Sandbox Code Playgroud)
这可能适用于与之相关的类Stream.但是,这一般适用吗?