Skip to content

Instantly share code, notes, and snippets.

@GSoster
Last active September 22, 2017 14:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GSoster/caf4a73995647f434e87b67fedfd56a4 to your computer and use it in GitHub Desktop.
Save GSoster/caf4a73995647f434e87b67fedfd56a4 to your computer and use it in GitHub Desktop.
using-keyword-blog-post
{
StreamReader sr = new StreamReader(filename);
try
{
String text = sr.ReadToEnd();
return text;
}
finally
{
if (sr != null)
((IDisposable)sr).Dispose();
}
}
public string ReadFromFile(string filename)
{
using (StreamReader sr = new StreamReader(filename))
{
String text = sr.ReadToEnd();
return text;
}
}
public Product GetProductById(int productId)
{
using (ProductContext db = new ProductContext)
{
return db.Products.SingleOrDefault(p => p.Id == productId);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment