Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
using (Idata Data = GetData())  {}

create object use using like this can help dispose object automatically,
are there any way to create multiple object like this at one time ?

What I have tried:

using (Idata Data = GetData(),   Idata Data1 = GetData1()  )  {}
using (Idata Data = GetData(); Idata Data1 = GetData1()  )  {}


this doesn't work.
Posted
Updated 24-Oct-17 21:08pm

Objects are automatically disposed of when not used, that is the job of the garbage collector.

usings job is for disposing objects which have non managed parts like references to underlying OS handles which take up non managed memory and can be released when not used outside the using block.

Generally you don't need using for your own classes, the objects which need it are usually specified in the documentation for them.
 
Share this answer
 
One after the other:
C#
using (Idata Data = GetData())
{
    using (Idata Data1 = GetData1())
    {
        // your code here
    }
}
 
Share this answer
 
Comments
Richard Deeming 25-Oct-17 11:46am    
If they're sharing the same lifetime, it's cleaner to not indent the inner using:
using (Idata Data = GetData())
using (Idata Data1 = GetData1())
{
    // your code here
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900