Missing the 'with' keyword in C#

14,482

Solution 1

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.

Solution 2

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.

Solution 3

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";
    ...
});

Solution 4

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;
}

Solution 5

For these solutions:

// ....
// (class Common)

public static void With<T>(T property, Action<T> action) {
    action(property);
}

// ...
// usage somewhere ...    
Person person = GetPerson();
Common.With(person, p => { p.Name = "test", p.Age = "123" });

It just seems we are aliasing the variable with "p". As solutions go, I found it easy enough to keep the variable name short, this sort of solution with a "With" generic doesn't buy any elegance.

Ideally, we'd all like to see some reworking of the syntax so the usage is similar to how initialization of multiple properties works today:

Person person = new Person() { Name = "test", Age = "123" };
Share:
14,482
Matt Davis
Author by

Matt Davis

My wife and I were married in 1994, and we have four beautiful children. I'm an avid Dallas Cowboys fan, hence my gravatar.

Updated on June 03, 2022

Comments

  • Matt Davis
    Matt Davis almost 2 years

    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.