This repository is the sample code developed to practice the using statement.
-
Generate new User class
public class User: IDisposable
and implement de interface on the class
private bool disposedValue = false; // Para detectar llamadas redundantes protected virtual void Dispose(bool disposing) { if (!disposedValue) { disposedValue = true; } } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); }
-
In
Main
, we can create a new User with using statementusing (User u = new User()) { // Some code }
- Create a new instance of a managed resource
and to release this resource:
StreamWriter swriter = new StreamWriter(path + @"\test.txt", true);
swriter.Dispose();