Why doesn't C# have VB.NET's 'with' operator?

Why doesn't C# have VB.NET's 'with' operator?

Rate This
  • Comments 38

Many people, including the C# language designers, believe that 'with' often harms readability, and is more of a curse than a blessing. It is clearer to declare a local variable with a meaningful name, and use that variable to perform multiple operations on a single object, than it is to have a block with a sort of implicit context.

For more information, see the Ask a C# Language Designer page.

[Author: Jon Skeet]

Leave a Comment
  • Please add 4 and 4 and type the answer here:
  • Post
  • We will have namespace aliases which I think is BAD, that coupled with lambda functions. C# is becoming Turd# whats next, partial methods? Dont even go there.
  • Not having with in C# is about the only thing that I miss from VB. If you think with caues that much confusion then what about using? Shouldn't we really be spelling out the enitire namespace evertytime we use a type?
    Of course with can be abused(like everything else) but if used right then it is a quite nice construct to save repetitive typing IMHO.

  • If you really miss it, you can create a local variable with a single character name. At that stage you only have to type one more character per line - is that really a hardship?
  • I'm with Christoffer: when I program C#, I want the With statement. When scoped properly, I find it very handy. Of course, it can be abused, such as when you have nested With statements. That being said, if each language had the same features, would we need different languages?
  • local variable with single character name? If the argument is for readability, this doesn't seem to fit
  • Jon,

    Doesn't creating a short variable name cause code readability problems and isn't that the problem the C# team is trying to avoid with not including the With statement?

    Personally, I find code that uses the With statement very readable.
  • Yes, it does. However, if someone *wants* with and the lack of readability it gives, presumably that isn't an issue for them. My point is that the lack of this "feature" really can't hurt *very* much for those who want it.
  • If I had to single out one thing I miss in C# (I'm coming from Delphi) - it would surely be the lack of 'with' statement.

    Mind you, having support for multiple structures in 'with' statement, like 'with A,B do' is abomination.
  • Delphi (i.e. Pascal) with statement has one very bad side effect. It is possible to alter meaning of a unit by adding a field to a structure in another unit. New fied can overlap local variables. This is not easy to track, because source control says that file was not changed recently.
  • Being a paying customer and not, admittedly, a purist who thinks he knows how everyone else should code, I'd like to see the With operator in a future release of C#.

    Just don't force the people who fuss over whether a black sweater goes better with their khakis than a dark blue one to use it, whatever you do!
  • The closest C# equivalent to VB's With statement is illustrated in the following example:

    using (DataGrid x = DataGridList)
    x.GridLines = GridLines.Both;
    x.BackColor = System.Drawing.Color.White;
    x.BorderStyle = BorderStyle.None;
    x.ShowFooter = false;
    x.ShowHeader = true;
    x.AllowPaging = true;
    x.AllowCustomPaging = true;
    x.AllowSorting = true;
    x.CellPadding = 4;
  • Jon, it'd be better for the debate if you stopped saying that 'with' gives a lack of readability. Clearly people's opinions vary on this one.
  • However Using(x) only works if x implements the IDispose interface.

    From a language "flavor" point of view (which is admittedly very subjective) it sort of feels right to me that VB, which tries to make your life easier at the expense of absolute purity of the language (see RaiseEvent for an example), would support With while C# does not. It's a bit odd that the result is that in this case VB code can be more terse than C#. I actually find it stranger that C# has the using() statement, which seems to be more in keeping with VB's philosophy.
  • WITH is horrible. It's only a problem for people that haven't programmed in a real language before; VB would be better off for those people anyhow. Think about it VB lovers, WITH "saves so much space", but you also use THEN and END IF as opposed to { and }. THEN and END IF appear MUCH more often than WITH ever should. If you rely on mechanisms like WITH, then you should seriously reconsider how you design your software, if you do at all. If I had to complain about C# lacking features, I would be asking where C-style Unions are. Splitting packets manually rather than by a union of an array and a struct is a pain
  • Uh... respect?
    As you can see here, there are quite a few people who like 'with', and Mr Clifton, you just disrespected ALL of them - so anyone who likes 'with' is a terrible programmer, and anyone who programs in VB is not a 'real' programmer? This sounds like a post that belongs on a Linux board (in keeping with Mr Clifton's blanket statements)
Page 1 of 3 (38 items) 123