views:

901

answers:

7

I was looking at the online help for the Infragistics control library today and saw some VB code that used the With keyword to set multiple properties on a tab control. It's been nearly 10 years since I've done any VB programming, and I had all but forgotten that this keyword even existed. Since I'm still relatively new to C#, I quickly went to see if it had a similar construct. Sadly, I haven't been able to find anything.

Does C# have a keyword or similar construct to mimic the functionality provided by the With keyword in VB? If not, is there a technical reason why C# does not have this?

EDIT: I searched for an existing entry on this before asking my question, but didn't find the one Ray referred to (here). To refine the question, then, is there a technical reason why C# does not have this? And Gulzar nailed it - no, there are not a technical reason why C# does not have a With keyword. It was a design decision by the language designers.

A: 

No, it was a conscious decision made by the C# dev team. People have mixed feelings about the 'with' keyword because it can degrade code readability if abused (nesting with's).

Ed Swangren
I wasn't aware you could nest "with" statements. That would reduce readability. However, like the GoTo and many other programming constructs, if used well, is it really all that bad?
Kibbee
I think it can be nice if used properly.
Ed Swangren
A: 

I don't imagine that there's a technical reason it doesn't exist. What I would say though, is that the reason that it doesn't exist is that it's a programming construct that exists solely for the purpose of cutting down on typing. With things like intellisense, and copy and paste, the world doesn't really have much demand for features like this anymore. I use VB.Net, and can't recall if it's even still supported. Never really felt the need to use it.

Kibbee
+19  A: 

This is what C# program manager has to say: Why doesn't C# have a 'with' statement?

  • Small or non-existent readability benefits. We thought the readability benefits were small or non-existent. I won't go as far as to say that the with statement makes code less readable, but some people probably would.

  • Increased language complexity. Adding a with statement would make the language more complex. For example, VB had to add new language syntax to address the potential ambiguity between a local variable (Text) and a property on the "with" target (.Text). Other ways of solving this problem also introduce language complexity. Another approach is to push a scope and make the property hide the local variable, but then there's no way to refer to the local without adding some escape syntax.

  • C++ heritage. C++ has never had a with statement, and the lack of such a statement is not generally thought to be a problem by C++ developers. Also, we didn't feel that other changes -- changes in the kind of code people are writing, changes in the platform, other changes in the language, etc. -- made with statements more necessary.

Gulzar
@Gulzar - Thanks for the link. It 'scratched the itch,' although I disagree with Mr. Wiltamuth's first two lines of reasoning. The with keyword, for me, has less to do with readability than it does about making the code easier to write...
Matt Davis
... And I find it difficult to believe that a with keyword is anywhere near the complexity involved with LINQ, anonymous methods, etc., but I digress. Thanks again for the link.
Matt Davis
glad to help Matt.
Gulzar
You are right on the money, but the explanation is weak. C++ heritage didn't include a whole bunch of stuff that is in the modern data C#.
AngryHacker
+8  A: 

In C# 3.0, you can use object initializers to achieve a similar effect when creating objects.

var control = new MyControl
{
    Title = "title",
    SomeEvent += handler,
    SomeProperty = foo,
    Another = bar
};

Rather than:

var control = new MyControl();
control.Title = "title";
control.SomeEvent += handler;
control.SomeProperty = foo;
control.Another = bar;

Note that, although this syntax was introduced in C# 3.0, you can still use it with the 2.0 framework, it's just syntactic sugar introduced by the compiler.

Matt Olenik
True, but you can only do this when you create an object. There is no mechanism if the object already exists. Thanks for the reminder, though.
Matt Davis
Yes, thank you. I further clarified that in my post.
Matt Olenik
+3  A: 

No, the "with" keyword was intentionally left out of the language.

If you have a lengthy name of reference, you can easily make a shorter reference to it using a variable, and even give it a limited scope:

{
   SomeClass r = Some.Lengthy.Path.To.Get.To.A.Referece;
   r.Some = 42;
   r.Properites = "none";
   r.To = 1;
   r.Set = null;
}
Guffa
+3  A: 

It is not idiomatic c#, but if you really want a with equivalent, you could do this:

Person MyPersonWithALongName = new Person();
MyUtils.With(MyPersonWithALongName, p => {

    p.Name = "George";
    p.Address = "123 Main St";
    ...

});

class MyUtils {

    public static void With<T>(T x, Action<T> do) {
        do(x);
    }
}

Update:
It occurred to me that you could trivially make this more concise by turning it into an extension method, perhaps renaming it "Alias" or "As" for reabability:

MyPersonWithALongName.Alias(p => {
    p.Name = "George";
    p.Address = "123 Main St";
    ...
});
Gabe Moothart