Can the C# using statement be written without the curly braces?
Yes, you can also put them in one using statement:
using (MemoryStream data1 = new MemoryStream(),
data2 = new MemoryStream())
{
// do stuff
}
The same rules apply when you omit the curly braces in a for
or an if
statement.
Incidentally if you reflect into the compiled code, the compiler decompiler adds the braces.
From C# 8 you can also use this syntax without any braces:
using var foo = new Foo();
var bar = foo.Bar();
foo
will then be disposed at end of its scope (usually at the end of a method) - so be mindful where & when to use.
Reference - https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement#example