C# 3.0 Features: Object Initializers

C# 3.0 is just around the corner so I thought I'd start writing about a few of the features that it exposes and provide quick and concise examples of how they can be used.  Many of the new features rely on the compiler to generate code that you would have had to write in the past. This can result in significantly less code compared to C# 2.0 syntax in many cases.  Here's a list of some of the key features available in C# 3.0:

  • Object Initializers
  • Anonymous Types
  • Automatic Properties
  • Extension Methods
  • Lambda Expressions
  • LINQ
  • Collection Initializers

In this post I'll discuss the fundamentals of object initializers in C# 3.0.  To get started, let's look at the standard way of initializing an object with data in C# 2.0 using constructors.  The following example creates a Person object and passes three values to its constructor.

Person p = new Person("John", "Doe", "602-123-1234");

As mentioned, C# 3.0 now supports the concept of "object initializers" which means you can easily assign data to specific properties in a type with having to create an explicit constructor (you can of course still create constructors as well). The standard C# { and } brackets are used to create object initializers.  Here's an example of using an object initializer to assign property data to a Person type.  It's nice because doing the same thing without using a constructor in C# 2.0 would have resulted in around 5 lines of code which is too many for something simple like property value assignments.

Person p = new Person() {FirstName="John",LastName="Doe",Phone="602-123-1234",City="Phoenix"};

If the Person type defines a sub object as a property you can even use object initializers to create the sub object. Here's an example of defining an Address object:

Person p = new Person()
{
    FirstName = "John",
    LastName = "Doe",
    Address = new Address()
    {
        Street = "1234 St.",
        City = "Phoenix"
    }
};


If you've used JavaScript Object Notation (normally referred to as JSON) in AJAX or other client-side applications, you'll recognize this style of syntax.  It's simple, compact, and easy to use.  Although I formatted the example above onto multiple lines to maximize readability, it could certainly be compacted quite a bit if desired. 

In the next post I do on C# 3.0 features I'll cover anonymous types and explain how they answer a question I'm often asked in .NET language classes I teach:  "Why do I have to define a type name twice when creating an object?".

Published Sunday, September 09, 2007 10:19 PM by dwahlin
Filed under: ,

Comments

# 16 Links Today (2007-09-10)

Monday, September 10, 2007 11:20 AM by 16 Links Today (2007-09-10)

Pingback from  16 Links Today (2007-09-10)

# re: C# 3.0 Features: Object Initializers

Tuesday, September 11, 2007 4:26 PM by Damien Guard

I believe the idea of a constructor is to give consumers of your class a clear picture as to the essential information (often properties) that are required to use your object.  The fact they set the properties for you is more of a side-effect.

There must be a risk that object initializers will mean some developers won't see the point in providing constructors and tell people just to set the properties which will leave consumers wondering which properties are essential and which are optional.

[)amien

# re: C# 3.0 Features: Object Initializers

Tuesday, September 11, 2007 5:29 PM by dwahlin

Damien,

I agree with you.  It's up to the developer though to decide if they want to provide constructors or not and to know what's best for essential properties.  This feature will definitely make it more difficult for developers to know whether or not constructors should be used or not. I completely agree with what you're saying.  However, it's a very useful and productive feature that certainly doesn't stop the use of constructors.

# Object Initializers in C# 3.0

Wednesday, September 12, 2007 5:45 PM by Blake Niemyjski

The other day I was doing my usual reading and came across some really cool information on Dan Wahlin's

# PaulStovell.NET » Object Initializers - the end of convenience constructors?

Pingback from  PaulStovell.NET » Object Initializers - the end of convenience constructors?

# What’s new in C# v3

Saturday, December 01, 2007 9:56 AM by Happy Coding :)

Do you want to know what's new in the 3rd version of the lovely C# :) ? just check the links below: ...

# C# 3.0 Features: Automatic Properties

Tuesday, December 04, 2007 2:57 PM by Dan Wahlin's WebLog

.NET 3.5 is out which means all of the great features available in C# 3.0 are available to use now. Here

# C# 3.0 Features: Extension Methods

Friday, January 25, 2008 4:15 PM by Dan Wahlin's WebLog

.NET 3.5 is out which means all of the great features available in C# 3.0 are available to use now. 

# New T-SQL 2008 Syntax

Tuesday, February 03, 2009 11:49 AM by BusinessRx Reading List

If you are using Microsoft SQL Server 2008 there are some cool new additions to the T-SQL syntax that

# New T-SQL 2008 Syntax

Tuesday, February 03, 2009 11:58 AM by ASPInsiders

If you are using Microsoft SQL Server 2008 there are some cool new additions to the T-SQL syntax that

# Hidden features of C# | nihi_l_istsBlog.Visible; //Title

Thursday, April 02, 2009 4:10 PM by Hidden features of C# | nihi_l_istsBlog.Visible; //Title

Pingback from  Hidden features of C# | nihi_l_istsBlog.Visible; //Title