Why is using "using" important? As well how to make a class that can be declared inside of a using statement. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why is using "using" important? As well how to make a class that can be declared inside of a using statement.

Can someone please explain. Thanks

1st Jun 2017, 4:48 AM
[No Name]
[No Name] - avatar
2 Answers
+ 2
It calls dispose on a disposable object as soon as it leaves the scope of the using statement or an exception occurs. This is useful for ensuring you dont forget to do it and have things like memory/resources being leaked
1st Jun 2017, 6:41 AM
aklex
aklex - avatar
+ 1
You need to implement the Idisposable interface which contains the Dispose methode. In the dispose methode you need to define what your class needs to do when the object is now longer in use. The using statement is esspecially handy when you need the object for a short defined moment. using { open file read data close file }
2nd Jun 2017, 7:10 PM
sneeze
sneeze - avatar