C# keyword Using implements Idisposable which provides a mechanism for releasing unmanaged resources.
Now i was going through this code
string txt = String.Empty;
using (StreamReader sr = new StreamReader(filename)) {
txt = sr.ReadToEnd();
}
and cant stop wondering, why is the keyword Using is used in this code while StreamReader is a Managed resource and it is the responsibility of the garbage collector to free up the objects memory after its scope gets over.
So my question is,
- Is the above code simply an explicit way of handling to free up resources
- As per my understanding any methods or classes which we use under the .net framework is a managed code, then is not StreamReader also falls under managed code.
- Am i missing something on the Managed\Unmanaged code