43

Possible Duplicate:
Equivalence of “With…End With” in c#?

There was one feature of VB that I really like...the With statement. Does C# have any equivalent to it? I know you can use using to not have to type a namespace, but it is limited to just that. In VB you could do this:

With Stuff.Elements.Foo
    .Name = "Bob Dylan"
    .Age = 68
    .Location = "On Tour"
    .IsCool = True
End With

The same code in C# would be:

Stuff.Elements.Foo.Name = "Bob Dylan";
Stuff.Elements.Foo.Age = 68;
Stuff.Elements.Foo.Location = "On Tour";
Stuff.Elements.Foo.IsCool = true;
Community
  • 1
  • 1
Bob Dylan
  • 4,393
  • 9
  • 40
  • 58

3 Answers3

51

Not really, you have to assign a variable. So

    var bar = Stuff.Elements.Foo;
    bar.Name = "Bob Dylan";
    bar.Age = 68;
    bar.Location = "On Tour";
    bar.IsCool = True;

Or in C# 3.0 and above:

    var bar = new FooType
    {
        Name = "Bob Dylan",
        Age = 68,
        Location = "On Tour",
        IsCool = True
    };

    Stuff.Elements.Foo = bar;
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • 12
    You are treating 'Stuff.Elements.Foo' as a type. Is that how it is being used in the VB code, or is it actually a reference to a nested variable? The technique is perfectly valid but I think your code isn't quite right. – quark Jul 24 '09 at 01:21
  • 4
    I wrote a [blog](http://blog.baltrinic.com/software-development/dotnet/c-equivalent-for-visual-basic-with-statement) some time back on how to simulate a with statement in c# using extension methods and lambda expressions. – Kenneth Baltrinic Jul 24 '11 at 21:41
  • 1
    @Kenneth: That's pretty clever, although it could be argued that `Object.Child.Grandchild.GreatGrandchild.Property1 = 1;` violates the Law of Demeter. – Robert Harvey Jul 24 '11 at 21:48
  • 2
    @Robert, _maybe_ but that's a topic for another thread. – Kenneth Baltrinic Jul 24 '11 at 22:05
  • The seconds part of this answer is invalid, `Stuff.Elements.Foo` is a variable. If it's a type - then you're missing `new` – Alex from Jitbit Nov 06 '21 at 20:36
  • "Not really". Actually not at all. That is different and only works in initialisers. The VB version was more commonly used with objects already instantiated. – John Stock Aug 24 '22 at 12:28
9

Aside from object initializers (usable only in constructor calls), the best you can get is:

var it = Stuff.Elements.Foo;
it.Name = "Bob Dylan";
it.Age = 68;
...
Pavel Minaev
  • 99,783
  • 25
  • 219
  • 289
6

The closest thing in C# 3.0, is that you can use a constructor to initialize properties:

Stuff.Elements.Foo foo = new Stuff.Elements.Foo() {Name = "Bob Dylan", Age = 68, Location = "On Tour", IsCool = true}
foson
  • 10,037
  • 2
  • 35
  • 53
  • 2
    You are treating 'Stuff.Elements.Foo' as a type. Is that how it is being used in the VB code, or is it actually a reference to a nested variable? The technique is perfectly valid but I think your code isn't quite right. (I've copied quark's comment here as it applies to this answer too) – MarkJ Jul 24 '09 at 08:24