RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


What's New in C# 3.0? Part 1 : Page 3

The release of Visual Studio 2008 updates C# to version, 3.0, with several key language enhancements and LINQ support. Part one of this series will walk you through implicit typing, automatic properties, and other time-saving enhancements.

Collection Initializers
Besides initializing objects at time of instantiation, you can also instantiate a collection of objects. For example, suppose you have a collection of Contact objects and want to keep them in a generic List object. In that case, you'd do this:

   List<Contact> Contacts = new List<Contact> ();
   Contacts.Add(new Contact { Name = "John", YearofBirth = 1980 });
   Contacts.Add(new Contact { Name = "Mary", YearofBirth = 1986 });
   Contacts.Add(new Contact { Name = "Richard", YearofBirth = 1948 });
Instead of using the Add() method multiple times, you can simply use the collection initializers, like this:

   List<Contact> Contacts = new List<Contact>() {
      new Contact { Name = "John", YearofBirth = 1980 },
      new Contact { Name = "Mary", YearofBirth = 1986 },
      new Contact { Name = "Richard", YearofBirth = 1948 } 
Anonymous Types
A new feature in C# 3.0 is anonymous types. Anonymous types allow you to define data types without having to formally define a class. Consider the following example:

            var contact1 = new
                id = "54321",
                Name = "Wei-Meng Lee",
                email = "weimenglee@learn2develop.net"
Here, contact1 is an object with three properties: id, Name, and email (see Figure 2).

Figure 2. IntelliSense Knows: IntelliSense automatically knows the properties contained with the contact1 object.
Figure 3. The Property Name: The property name will take the name of the variable name in an anonymous type.

Notice that there's no need to define a class containing the three properties. Instead, the object is created and its properties are initialized with their respective values.

Author's Note: In C#, anonymous types are immutable, which means that all their properties are read-only, unlike in VB 9.0, which requires the use of the Key keyword to denote immutable properties.

You can use variables when assigning values to properties in an anonymous type, like this:

            var Name = "Wei-Meng Lee";
            var Email = "weimenglee@learn2develop.net";
            var contact1 = new { id = "54321", Name, Email };
In this case, the name of the properties will take the name of the variable, as shown in Figure 3.

However, the following is not allowed:

   var contact1 = new {"54321", "Wei-Meng Lee", 
                       "weimenglee@learn2develop.net" };
Hence, when assigning a literal value to a property in an anonymous type, you must use an identifier, like this:

            var contact1 = new
                id = "54321",
                Name = "Wei-Meng Lee",
                email = "weimenglee@learn2develop.net"
So, how are you supposed to use anonymous types? Well, they allow you to shape your data from one type to another. You'll learn about this in Part 2, when we'll discuss LINQ support.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date