Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a VB.NET "With" statement in C#? [duplicate]

Tags:

syntax

c#

vb.net

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?

like image 421
Arion Avatar asked Feb 28 '12 13:02

Arion


1 Answers

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");
like image 137
Guffa Avatar answered Nov 14 '22 23:11

Guffa