Visual Basic 关键字的 C# 等效项:“With” ...“结束于”?
在 Visual Basic 中,如果要更改单个对象的多个属性,可以使用 With/End With
语句:
Dim myObject as Object
// ' Rather than writing:
myObject.property1 = something
myObject.property2 = something2
// ' You can write:
with myObject
.property1 = something
.property2 = something2
...
End With
我知道 C# 在创建新对象时可以做到这一点:
Object myObject = new Object { property1 = something, property2 = something2, ...};
但是我该怎么做如果 myOject
已经创建(就像 Visual Basic 所做的那样)?
In Visual Basic, if you are going to change multiple properties of a single object, there's 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 C# can do it when creating a new object:
Object myObject = new Object { property1 = something, property2 = something2, ...};
But how do I do that if myOject
is already created (like what Visual Basic is doing)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您无法在 C# 中执行此操作。
此功能特定于 VB,在 C# 中您可以使用的最接近的是您所描述的对象初始值设定项。
You cannot do this in C#.
This feature is specific to VB and the closest you can come in C# is the object initializer like you describe.
这个怎么样?
How about this?
如果您想避免大量输入,您可以为对象指定一个较短的名称:
If you're trying to avoid lots of typing you can give your object a shorter name:
@Mark Byers 答案 很好,但变量
x
将在设置属性后生效。并且您不能再次使用名称x
(在同一块中)。试试这个(并且此示例中的对象必须是引用类型):
@Mark Byers answer is good but the variable
x
will live after properties are set. And you can't use namex
again (in same block).Try this (And object must be reference type in this sample) :
为了向后兼容,VB.NET 包含了 VB6 的一些设计缺陷。虽然 Javascript 具有相同的设计缺陷(实际上是一个更糟糕的缺陷,因为它的
with
会导致更加模糊的构造),但大多数其他 C 语法语言没有,因此添加它到 C#。VB.NET includes some of VB6's design flaws for the sake of backward compatibility. While Javascript has the same design flaw (indeed an even worse one, as its
with
leads to more ambiguous constructs), most other C-syntax languages don't, so there's no backward-compatibility benefit in adding it to C#.如果“with”表达式是类类型,则“With”语句相当于创建该类型的新临时变量,初始化为“With”表达式,并在每个前导“.”之前。与该变量。然而,如果它是一个结构类型,事情就更复杂了。考虑一下代码(显然不是人们通常编写代码的方式,而是为了表达一个观点而编写的:
“With”语句有效地锁存了对 MyPoints(N) 的引用。即使 MyPoints 更改为其他数组,或者N 更改后,锁存的引用仍将指向执行 With 语句时相同的数组元素,如果声明了 Point 类型的局部变量 P 并获取 MyPoints(N),然后写入 PX。和 PY 中,写入只会命中本地副本 P,而不是更新数组 要在 C# 中实现类似的语义,必须使用本地变量来保存 MyPoints 和 N,或者放置 With 语句的内容。在具有 Point 类型的 ref 参数的匿名函数中,为了避免在运行时创建闭包,匿名函数还应该接受(可能是通过引用)来自外部作用域的任何局部变量。
If the "with" expression is a class type, the "With" statement is equivalent to creating a new temporary variable of that type, initialized to the "With" expression, and preceding each leading "." with that variable. If it is a structure type, however, things are more complicated. Consider the code (obviously not the way one would normally write something, but written as it is to make a point:
The "With" statement effectively latches a reference to MyPoints(N). Even if MyPoints is changed to some other array, or N is changed, the latched reference will still point to the same element of the same array as it did when the With statement was executed. If one declared a local variable P of type Point and grabbed MyPoints(N), and then write to P.X and P.Y, the writes would only hit the local copy P, rather than updating the array. To achieve similar semantics in C#, one would have to either use local variables to hold both MyPoints and N, or else place the contents of the With statement within an anonymous function which has a ref parameter of type Point. To avoid having to create a closure at run-time, the anonymous function should also accept, probably by reference, any local variables it will need from the outer scope.