Jump to content
  • Advertisement
Sign in to follow this  
CProgrammer

using in c#

This topic is 5036 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I just stumbled over this code: using (WinForm our_dx_form = new WinForm()) { Application.Run(our_dx_form); } Now Im curious as to what using is doing here and whats different between this and just WinForm our_dx_form = new WinForm(); Application.Run(our_dx_form); Im used to c++ and this doesnt tell me anything.

Share this post


Link to post
Share on other sites
Advertisement
"using" is a special keyboard to use on items that implement the IDisposable interface. You can tell these classes because they have a Dispose() method in them to free / clean up resources. At the end of the using() {} statement, C# will call the object's Dispose() method.

So using your code, this is what happens:

using (WinForm our_dx_form = new WinForm())
{
Application.Run(our_dx_form);
}
// our_dx_form.Dispose() is called because the item has gone out of scope.




You can also declare multiple items in the using statement like so:


using(Item one = new Item, Item two = new Item)
{
// Use them
}
// one.Dispose() and two.Dispose() are called because the items have gone out of scope.



Hope that's clear.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!