what is the use of using statement in c# ?
Using statement used for deleting managed objects.
Using statement used for calling dispose method of Idisposable interface properly by using try and finally pattern.
Using statement is used as a replacement to the C#'s finalize method.
None of the above.
using (TextReader tr = new StreamReader(@"C:\patients.txt"))
assume you are not using TextReader object without using or without wrapping it in using block
you must use try and finally pattern to ensure proper cleaning of unmanaged resources used by TextReader
object as shown below. If you observer definition of TextReader ( to see definition of a class in visual studio rightclick
on classname and click on GoTo Definition ) it is implementing IDisposable interface , hence you can say
that TextReader is using un managed resources .
TextReader tr1 = new StreamReader(@"C:\patients.txt");
string data = tr1.ReadToEnd();
In simple words using block is converted into equivalent try and finally pattern for calling dispose method appropriately .
Back To Top