Is there a C# With…End With statement equivalent?
The With…End With statement in VB allows you to execute a series of statements that epeatedly refer to a single object. Thus the statements can use a simplified syntax for accesing members of the object. For example:
With car
.Make = "Mazda"
.Model = "MX5"
.Year = 1989
End With
Code language: JavaScript (javascript)
Is there a C# syntax equivalent?
No. There is not. The closest thing to it would be the object initializers, but those are only for instantiating new objects, they cannot be used to update existing object instances, like the with statement can.
As an example, when creating a new object instance, you can use the object initializer:
var car = new Car
{
Make = "Mazda",
Model = "MX5",
Year = 1989
};
Code language: C# (cs)
But when updating the object, there is no equivalent simplified syntax. You would have to reference the object for each assignment or member call, like so:
car.Make = "Aston Martin"
car.Model = "DBS"
car.Year = 1967
Code language: C# (cs)