C # equivalent for Visual Basic keyword: 'With' ... 'End With'?

In Visual Basic, if you intend to change several properties of a single object, there is a With/End With statement:

 Dim myObject as Object // ' Rather than writing: myObject.property1 = something myObject.property2 = something2 // ' You can write: with myObject .property1 = something .property2 = something2 ... End With 

I know that C # can do this when creating a new object:

 Object myObject = new Object { property1 = something, property2 = something2, ...}; 

But how can I do this if myOject already created (for example, what does Visual Basic do)?

+7
c # language-design with-statement
source share
7 answers

You cannot do this in C #.

This feature is specific to VB, and the closest you can enter to C # is the object initializer, as you describe.

+11
source share

How about this?

 static class Extension { public static void With<T>(this T obj, Action<T> a) { a(obj); } } class Program { class Obj { public int Prop1 { get; set; } public int Prop2 { get; set; } public int Prop3 { get; set; } public int Prop4 { get; set; } } static void Main(string[] args) { var detailedName = new Obj(); detailedName.With(o => { o.Prop1 = 1; o.Prop2 = 2; o.Prop3 = 3; o.Prop4 = 4; }); } } 
+7
source share

If you are trying to avoid a lot of input, you can give your object a shorter name:

 var x = myObject; x.property1 = something; x.property2 = something2; 
+3
source share

Why doesn't C # have a VB.NET 'with' statement?

Many people, including C # designers, think that “c” often harms readability and is more a scourge than a blessing. It is clearer to declare a local variable with a meaningful name and use this variable to perform several operations on one object than to have a block with some implicit context.

@Jon Skeet

+3
source share

VB.NET includes some design flaws in VB6 for backward compatibility. While Javascript has the same design flaw (even worse, since its with leads to more ambiguous constructs), most other C syntax languages ​​do not, so there is no backward compatibility advantage when adding it to C #.

+1
source share

If the expression “c” is a class type, the operator “C” is equivalent to creating a new temporary variable of this type, initialized by the expression “C” and preceding each leading “.”. with this variable. However, if this is a type of structure, things get more complicated. Consider the code (obviously, not the way something is usually written, but written to make a point:

   With MyPoints (N) 'Array of Point
     N = SomeNewValue
     .X = MyPoints (N) .X
     .Y = MyPoints (N) .Y
   End with

The C operator effectively captures a link to MyPoints (N). Even if MyPoints is changed to some other array, or N is changed, the anchor link will still point to the same element of the same array as when executing the With statement. If you declare a local variable P of type Point and capture MyPoints (N), and then write to PX and PY, the records will only get into the local copy of P, and not update the array. To achieve similar semantics in C #, you had to either use local variables for storage as MyPoints and N, or place the contents of the With statement in an anonymous function with the ref parameter of type Point. To avoid creating a closure at runtime, the anonymous function should also accept, possibly by reference, any local variables that it needs from the outer scope.

0
source share

@Mark Byers answer is good, but the x variable will live after the properties are set. And you cannot use the name x again (in the same block).

Try this (And the object should be a reference type in this example):

 void Main() { var myObject1 = new Foo(); var myObject2 = new Hoo(); //elided... { var _ = myObject1; _.MyPropertyA = 2; _.MyPropertyB = "3"; } { var _ = myObject2; _.MyPropertyX = 5; _.MyPropertyY = "asd"; } } 
0
source share

All Articles