mercredi 1 mars 2017

Any better way to use if else statement while using an “using” block statement?

I have a scenario where I have two new objects in which only one has to be initialized according to the condition.

But I am using the “using” block statement for initializing a new object.

How can I achieve it? Please refer the below scenario.

int a;
string b;

if()//some codition
{
    using(MyClass c1 = new MyClass(a))
    { 
            SomeMethod();
    }
}
else
{
    using(MyClass c1 = new MyClass(b)
    {
             SomeMethod();
    }
}

Is there any better way to achieve this in single condition or any other way to reduce the code? because I am calling the same method in both condition.

Thanks in advance.

Regards, Anish

Aucun commentaire:

Enregistrer un commentaire