小编Sta*_*oyd的帖子

Can I safely use object initializers inside using statements?

I'm wondering if the use of object initializers inside using statements somehow prevents the correct disposal of the resource declared inside them, e.g.

using (Disposable resource = new Disposable() { Property = property })
{
   // ...
}
Run Code Online (Sandbox Code Playgroud)

I've read that object initializers are nothing but synctatic sugar, which the compiler translates to something similar to the following code:

MyClass tmp = new MyClass(); 
tmp.Property1 = 1;
tmp.Property2 = 2;
actualObjectYouWantToInitialize = tmp;
Run Code Online (Sandbox Code Playgroud)

Even if I may appear as a …

c# using-statement object-initializers

4
推荐指数
1
解决办法
77
查看次数

标签 统计

c# ×1

object-initializers ×1

using-statement ×1