C#等同于Visual Basic关键字:'With'...'End With'? [英] C# equivalent for Visual Basic keyword: 'With' ... 'End With'?
本文介绍了C#等同于Visual Basic关键字:'With'...'End With'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Visual Basic中,如果要更改单个对象的多个属性,则有一个With/End With
语句:
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
我知道C#在创建新对象时可以做到这一点:
I know C# can do it when creating a new object:
Object myObject = new Object { property1 = something, property2 = something2, ...};
但是如果已经创建了myOject
(如Visual Basic正在执行的操作)怎么办?
But how do I do that if myOject
is already created (like what Visual Basic is doing)?
推荐答案
您不能在C#中执行此操作.
此功能特定于VB,在C#中最接近的功能是您所描述的对象初始化程序.
This feature is specific to VB and the closest you can come in C# is the object initializer like you describe.
这篇关于C#等同于Visual Basic关键字:'With'...'End With'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文