How to instantiate an object and pass it to a method within the declaration of a "using" block
<p>I am using an external class which implements IDisposable and an external method which populates an instance of that class. </p>

<p>I am looking for a neat way to encapsulate the disposable object in a "using" statement but can't work out the syntax.</p>

<p>Let's say the class is ...</p>

<pre><code>class Something : IDisposable
</code></pre>

<p>And the function is ...</p>

<pre><code>void PopulateSomething(Something theObject)
</code></pre>

<p>I could do this ...</p>

<pre><code>Something myObject = new Something();
PopulateSomethingObject(myObject);
x = myObject.SomeNumber;
myObject.Dispose();
</code></pre>

<p>or I could also do this ...</p>

<pre><code>Something myObject = new Something();
using(myObject)
{
    x = myObject.SomeNumber;
}
</code></pre>

<p>but neither of the above are great because after disposal I could stupidly write something like this</p>

<pre><code>x = myObject.SomeNumber; // which will throw an exception
</code></pre>

<p>I suppose I could do this ...</p>

<pre><code>using(Something myObject = new Something())
{
    PopulateSomethingObject(myObject);
    x = myObject.SomeNumber();
}
</code></pre>

<p>But I would like to believe something like the following is possible but I can't figure out the syntax...</p>

<pre><code>using (Something myObject =&gt; PopulateSomethingObject(myObject = new Something()))
{
    x = myObject.SomeNumber();
}
</code></pre>

<p>I guess it's sort of a theoretical question because I don't really have a problem here but I am curious to know if it is possible. i.e. Instantiate the object and pass it to a method within the declaration of the "using(...){} block.</p>