I see this:
using (StreamWriter sw = new StreamWriter("file.txt"))
{
// d0 w0rk s0n
}
Everything I try to find info on is does not explain what this doing, and instead gives me stuff about namespaces.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You want to check out documentation for the using statement (instead of the using directive which is about namespaces).
Basically it means that the block is transformed into a
try/finallyblock, andsw.Dispose()gets called in the finally block (with a suitable nullity check).You can use a using statement wherever you deal with a type implementing
IDisposable– and usually you should use it for any disposable object you take responsibility for.A few interesting bits about the syntax:
You can acquire multiple resources in one statement:
You don’t have to assign to a variable:
You can nest them without braces; handy for avoiding indentation