What’s the C# equivalent to the With statement in VB? [duplicate]

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;

Leave a Comment