Should C# Offer the With Construct?

As repulsive as I find the syntax of VB .NET, I recently stumbled upon one of its language features which piqued my interest – the With keyword, for which no direct equivalent exists in C#. It basically allows you to write code like the following (in VB).

With New Car
    .Colour = "Red"
    .Name = "Bob"
    .Drive(20, Direction.South)
End With

There seems to be quite a lot of controversy over this language feature and I get the feeling I’m one of the few C# coders who sees anything positive in it. So I’ll justify what I like about it:

  • Brevity:
    As in the example, you can instantiate a class and refer to it right there using a single period. Or maybe instead of referring to a new Car, you want to refer to an object buried deep in a hierarchy (i.e. house.Suburb.City.Country), but you don’t want to type out the entire structure in each of the five consecutive lines where you use it? The counter-argument is of course to create a temporary variable with a shorter name and to use that instead… but I find this approach (in some cases) to be cleaner and preferable for other reasons too.
  • Readability:
    With this code, the intention is obvious. It says “Ok, I have my object and now I’m going to make use of it in this clearly delineated and semantically cohesive block of code”.
  • Scoping:
    In the above example, the car cannot be referred to outside the With block… and provided that you’re finished with it by that point, this approach makes perfect sense. You can achieve the same thing in C# by simply placing all the code inside some braces in order to induce a new scope. But again, I’d hardly consider that readable or obvious.

Now naturally, code readability is a fairly subjective matter, and I’m sure that many C# programmers would still balk at a C# version of a With construct. But is it really so different in its virtues and appearance to object initialisers? They, also, achieve brevity and readability by grouping together operations on a single object, while offering what is effectively a shorthand way of accessing it. And what about using blocks? While working towards their purpose of simplifying resource management, they often incidentally achieve the readability and scope limiting I mentioned above.

So it strikes me that VB With could be considered as a generalised version of existing C# constructs which serve more specific purposes. And I’d say that it could add some value if used judiciously, where existing constructs aren’t applicable and refactoring into methods would be overkill. But just as GOTO is a more generalised and versatile version of practically every control flow structure, and is all the more abuseable for it, I imagine that With could be easily misused/overused, leading to wretched code packed with atrocities such as hulking nested With blocks and contrived attempts to write every single method as a sequence of With clauses. So handle with care?

About Nathan Pitman

Undergrad software engineer at the University of Auckland
This entry was posted in C# and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *