Search code examples
c#refactoringusingusing-statement

How to use object initializers with using statements?


Is there any way to refactor this code to not have to use a temporary variable and still use the syntactic sugar associated with object initializers?

FrmSomeForm someTempForm = new FrmSomeForm()
{
    SomePropA = "A",
    SomePropB = "B",
    SomePropC = "C"
};
using (FrmSomeForm someForm = someTempForm)
{
    someForm.ShowDialog();
}

Solution

  • using (FrmSomeForm someForm = new FrmSomeForm(){
        SomePropA = "A",
        SomePropB = "B",
        SomePropC = "C"
    })
    {
        someForm.ShowDialog();
    }
    

    doesn't this work? oO