Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing the 'with' keyword in C# [duplicate]

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.

like image 872
Matt Davis Avatar asked Mar 02 '09 03:03

Matt Davis


People also ask

What is missing error in C?

In C language, 'statement missing' error occurs when you don't put a semicolon(;) at the end of a statement. So go to the line number where your compiler is showing error and see if there is any semicolon(;) missing. Hope this helps.

What is Call of Nonfunction error in C?

It means you are trying to call something that is not a function. For instance: C++ Copy Code. int c; c(); // this is a call to a nonfunction.

Is there a this keyword in C?

In C you do not have the this keyword. Only in C++ and in a class, so your code is C and you use your this variable as a local method parameter, where you access the array struct.

What is declaration syntax error in C?

E2141 Declaration syntax error (C++)Your source file contained a declaration that was missing a symbol or had an extra symbol added to it. Check for a missing semicolon or parenthesis on that line or on previous lines.


4 Answers

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.

like image 72
Gulzar Nazim Avatar answered Sep 26 '22 10:09

Gulzar Nazim


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.

like image 37
Matthew Olenik Avatar answered Sep 23 '22 10:09

Matthew Olenik


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";
    ...
});
like image 11
Gabe Moothart Avatar answered Sep 27 '22 10:09

Gabe Moothart


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;
}
like image 9
Guffa Avatar answered Sep 23 '22 10:09

Guffa