在C#中创建对象时,我们也可以(同时)填写其属性。这在VB.NET中是否可行?
例如:
MyObject obj = new MyObject
{
Name = "Bill",
Age = 50
};
答案 0 :(得分:9)
是的,有可能:
Dim obj As New MyObject With { .Name = "Bill", .Age = 50 }
两件重要的事情:
With
{ ... }
关键字
.Name
代替Name
对于集合初始值设定项,请使用From
关键字:
Dim obj as New List(Of String) From { "String1", "String2" }