Possible Duplicate: Equivalence of “With…End With” in C#?
So I don't know if this question has been asked before and I am not a big fan of VB.NET. But one syntax that I am missing in C# is the With
syntax.
In VB.NET you can write:
Dim sb As New StringBuilder
With sb
.Append("foo")
.Append("bar")
.Append("zap")
End With
Is there a syntax in C# that I have missed that does the same thing?
No, there isn't.
This was intentionally left out of C#, as it doesn't add much convenience, and because it can easily be used to write confusing code.
Blog post from Scott Wiltamuth, Group Program Manager for Visual C#, about the with keyword: https://web.archive.org/web/20111217005440/http://msdn.microsoft.com/en-us/vstudio/aa336816.aspx
For the special case of a StringBuilder
, you can chain the calls:
StringBuilder sb = new StringBuilder()
.Append("foo")
.Append("bar")
.Append("zap");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With