The next new feature of .NET 3.5 that I want to blog about is the addition of object and collection initializers. Here's the final simplified Student class definition from my previous post:
1: public enum Gender {
2: Female,
3: Male
4: }
5:
6: public class Student {
7: public DateTime BirthDate { get; set; }
8: public string FirstName { get; set; }
9: public Gender Gender { get; set; }
10: public string LastName { get; set; }
11: }
When creating a Student object in the past, I might have used code such as the following to initialize the object:
1: Student student = new Student();
2: student.FirstName = "Michael";
3: student.LastName = "Collins";
4: student.BirthDate = new DateTime(1975, 1, 31);
5: student.Gender = Gender.Male;
As an alternative, I might have enhanced my Student class to have constructors where I would pass in the values:
1: Student student = new Student("Michael", "Collins",
2: new DateTime(1975, 1, 31), Gender.Male);
The problem with the second approach that uses a constructor is that if I go back and add more properties to my Student class, then I'm required to modify the constructor by adding new parameters to the constructor. If I modify the constructor, then I have to go back and modify all of the code that uses the constructor. If third-parties are using my Student class, then they have to be notified so that they can change their code. Basically, there are a lot of code changes. The first approach, while using more lines of code, is more adaptable to the situation where I add more properties to the class without breaking existing code. However, the downside is that the constructor approach is easier and requires less code. It can be tedious, especially when you're creating multiple objects, to sit there and write the same property setter code over and over again. But .NET 3.5 helps us out just a little with object initializers.
Object initializers are a nice mix of property setters and constructor syntax. Using an object initializer, I can rewrite the above code samples like this:
1: Student student = new Student { FirstName = "Michael", LastName = "Collins",
2: BirthDate = new DateTime(1975, 1, 31), Gender = Gender.Michael };
A lot simpler, isn't it?
Where it gets even better is when we can mix object initializers with collections. Imaging the scenario where I want a collection of students. If I take my first approach to initializing the Student objects to create a collection of students in memory, I have to write a lot of code to initialize each collection member. With collection initialization and object initialization, I can write the following:
1: var students = new List<Student> {
2: new Student { FirstName = "Michael", LastName = "Collins", ... },
3: new Student { FirstName = "Scott", LastName = "Williams", ... },
4: new Student { FirstName = "Joe", LastName = "Wilson", ... },
5: new Student { FirstName = "Jack", LastName = "Benson", ... }
6: };
The above code sample also is using the var shortcut for local variable definitions. This is another new feature in .NET 3.5, in that you still get the same type-safe code, but you don't need to declare the type of each local variable. Instead, at compile time, the compiler infers the actual type of the variable.
To give you an idea of how easier collection initialization makes building collections, here's how it would have been done previously. Of course, I'm cheating here as well, because I'm still using object initialization.
1: var students = new List<Student>();
2: students.Add(new Student { FirstName = "Michael", LastName = "Collins", ... });
3: students.Add(new Student { FirstName = "Scott", LastName = "Williams", ... });
4: students.Add(new Student { FirstName = "Joe", LastName = "Wilson", ... });
5: students.Add(new Student { FirstName = "Jack", LastName = "Benson", ... });
While we end up with more-or-less the same amount of code, I very much prefer the first example that uses collection initialization when you know what objects that you want to put into the collection.
Technorati tags:
.net,
c#
Be the first to rate this post
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5