dotnet thoughts 

a dotnet developer's technical blog

Object Initializers in VB.Net

In my last post I have used a special syntax for creating instance of Person Class.
VB.Net

Dim p as New Person With {.Name = “Person1″, .Age = 20}

C#

Person p = New Person {Name = “Person1″, Age = 20};

Which is equivalent to
VB.Net

Dim oPerson as new Person
oPerson.Name = "Person1"
oPerson.Age = 20

C#

Person oPerson = new Person();
oPerson.Name = "Person1";
oPerson.Age = 20;

This is another new feature introduced in VB.Net 9.0, called Object Initializers. You can get more information about this in MSDN. Actually I am planning to write a post on the feature newly available in VB.Net 9.0

Links : VB.Net and C#

No Responses to “Object Initializers in VB.Net”

  1. Actually, the C# code is equivalent to something like this:


    Person tempPerson = new Person();
    tempPerson.Name = "Person1";
    tempPerson.Age = 20;
    Person oPerson = tempPerson;

    I do not know about the VB.Net code, but I expect a similar situation.

    Comment by lagattack — August 18, 2008 @ 4:36 pm

  2. lagattack, Sorry I can’t understand your comment. :(

    Comment by anuraj — August 22, 2008 @ 5:13 pm

RSS feed for comments on this post. TrackBack URL

Leave a Response

*