Understanding Object Initialization Syntax

As of .NET 3.5, it is possible to set property values at the time of object construction using object
initialization syntax
. This can be a handy time saver as you are able to condense multiple lines of code into a
single statement.

Assume the following Point type, which models an {X, Y} position. Here, you are encapsulating the private
data with public properties. However, you can also set public field data with object initialization syntax. The
VB
Point would have the same functionality using VB-specific syntax.


// C#
public struct Point
{
private int xPos, yPos;

public Point(int x, int y)
{  xPos = x; yPos = y; }

public int X
{
get { return xPos; }
set { xPos = value; }
}
public int Y
{
get { return yPos; }
set { yPos = value; }
}

public override string ToString()
{ return string.Format("[{0}, {1}]", xPos, yPos); }        
}



Now consider the following usage of the Point type in C# code. The first two Point allocations work under
any version of C#. The final two allocations make use of object initialization syntax.


static void Main(string[] args)
{
// Make a Point by setting each property manually.
Point firstPoint = new Point();
firstPoint.X = 10;
firstPoint.Y = 10;

// Make a Point via a custom constructor.
Point anotherPoint = new Point(20, 20);

// These Point variables are only supported in C# 3.0.
// Make some Point types using object init syntax.
var yetAnotherPoint = new Point { X = 30, Y = 30 };
Point finalPoint = new Point { X = 30, Y = 30 };
}


In VB, object initialization syntax may remind you of the With construct. Rather than authoring a With
scope to set each property, you can now do so directly ‘inline’. Notice that each property is prefixed with a
dot operator.


' VB
Sub Main()
' Object init syntax in VB 9.0.
Dim yetAnotherPoint = New Point With {.X = 30, .Y = 30}
Dim finalPoint As Point = New Point With {.X = 30, .Y = 30}
End Sub


In the previous code example, yetAnotherPoint and finalPoint were created via the default constructor. When
using the default constructor of the type, you are not required to specify a set of parentheses. However, the
following code is also permissible:

// C#
var yetAnotherPoint = new Point() { X = 30, Y = 30 };


' VB
Dim yetAnotherPoint = New Point() With {.X = 30, .Y = 30}

If you specify a custom constructor, be aware that the properties will be set after the constructor call.
Therefore, object initialization syntax could override your initial constructor settings. For example, the
following
Point will display an X value of 100 and a Y value of 100, despite the supplied constructor values.


// C#
Point pt = new Point(10, 16) { X = 100, Y = 100 };
Console.WriteLine(pt);










' VB
Dim pt As Point = New Point(10, 16) With {.X = 100, .Y = 100}
Console.WriteLine(pt)


Object initialization syntax can be used when properties expose complex types, as well. Consider the
following
Rectangle type definition, which contains two member variables of type Point. Again, VB code
would be functionally identical.


// C#
public class Rectangle
{
private Point topLeft = new Point();
private Point bottomRight = new Point();

public Point TopLeft
{
get { return topLeft; }
set { topLeft = value; }
}
public Point BottomRight
{
get { return bottomRight; }
set { bottomRight = value; }
}

public override string ToString()
{
return string.Format("[TopLeft: {0}, {1}, BottomRight: {2}, {3}]",
 topLeft.X, topLeft.Y, bottomRight.X, bottomRight.Y);
}
}



You could now create Rectangle types as follows:


// C#
Rectangle myRect = new Rectangle
{
TopLeft = new Point { X = 10, Y = 10 },
BottomRight = new Point { X = 200, Y = 200}
};









' VB
Dim myRect As New Rectangle With { _
.TopLeft = New Point With {.X = 10, .Y = 10}, _
.BottomRight = New Point With {.X = 200, .Y = 200}}


This is simply a shorthand notation for the following ‘old school’ approach. Object initialization syntax
allows you to hydrate an object’s state in a more concise and functional manner.


// Old school approach (C#).
Rectangle r = new Rectangle();
Point p1 = new Point();
p1.X = 10;
p1.Y = 10;
r.TopLeft = p1;

Point p2 = new Point();
p2.X = 200;
p2.Y = 200;
r.BottomRight = p2;
Understanding Object Initialization Syntax
Table of Contents
Copyright (c) 2008.  Intertech, Inc. All Rights Reserved.  This information is to be used exclusively as an
online learning aid.  Any attempts to copy, reproduce, or use for training is strictly prohibited.