什么是VB.net相当于这个C#命令

时间:2012-08-20 03:41:20

标签: vb.net c#-3.0

var safeMode = new SafeMode(other) { WMode = "majority" };

我在http://www.mongodb.org/display/DOCS/CSharp+Driver+Tutorial#CSharpDriverTutorial-C%23Driverversionv1.4.x

中多次看到此表单

{ WMode = "majority" }是我不熟悉的。它是代码,集合初始化器还是什么?

它的作用是调用SafeMode的构造函数。我不知道你可以在那之后添加括号。那是“代码”还是什么?

3 个答案:

答案 0 :(得分:2)

它被称为object初始化(MSDN - Overview of Visual Basic 9.0 - object-initializer)。

Dim objSafeMode = New SafeMode(other) With { .WMode = "majority" }

答案 1 :(得分:1)

这是C#语法的一部分。以下是编程指南的链接:

http://msdn.microsoft.com/en-us/library/bb384062.aspx

示例:

Cat cat = new Cat { Age = 10, Name = "Fluffy" };

答案 2 :(得分:1)

语法称为Object Initializers。

等效的vb.net代码可以是这个完整的代码

Dim safeMode as New SafeMode(other)
With safeMode
 .WMode = "Majority"
End With

或使用对象初始化程序语法。

Dim safeMode as New SafeMode(other) With { .WMode = "Majority" }

http://blogs.msdn.com/b/wriju/archive/2008/02/05/vb-net-9-0-object-and-array-initializers.aspx