Why to use “using” in C#?
Loading
Why to use “using” in C#?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Brahma Prakash ShuklaPosted Nov 1, 2022, 2:31 PM
“Using” statement calls – “dispose” method internally, whenever any exception occurred in any method call and in “Using” statement objects are read only and cannot be reassignable or modifiable.
Rajanikant HawaldarPosted Nov 2, 2022, 9:48 AM
The reason for the using statement is to ensure that the object is disposed as soon as it goes out of scope, and it doesn't require explicit code to ensure that this happens.
Microsoft documentation states that using has a double function, both as a directive and in statements. As a statement, as it was pointed out here in other answers, the keyword is basically syntactic sugar to determine a scope to dispose an IDisposable object. As a directive, it is routinely used to import namespaces and types. Also as a directive, you can create aliases for namespaces and types.
Naresh BeniwalPosted Nov 1, 2022, 2:35 PM
https://www.c-sharpcorner.com/UploadFile/ff0d0f/using-keyword-in-C-Sharp/