In C# when creating an object we can also (at the same time) fill out its properties. Is this possible in VB.NET?
For example:
MyObject obj = new MyObject
{
Name = "Bill",
Age = 50
};
Yes, it's possible:
Dim obj As New MyObject With { .Name = "Bill", .Age = 50 }
Two important things:
With
keyword after class name and before { ... }
.Name
instead of Name
For collection initializers use From
keyword:
Dim obj as New List(Of String) From { "String1", "String2" }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With