Firstly, we will talk about object initalizers. Take a look at the following code which is what you would normally write in C#:
//C# 2.0Nothing too complicated there or out of the ordinary.
public class Person
{
private string name = string.Empty;
private int age = -1;
private bool married = false;
public Person(string name)
{
this.name = name;
}
public Person(string name, int age)
{
this.name = name;
this.age = age;
}
public Person(string name, int age, bool married)
{
this.name = name;
this.age = age;
this.married = married;
}
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public bool Married
{
get
{
return married;
}
set
{
married = value;
}
}
public int Age
{
get
{
return age;
}
set
{
age = value;
}
}
}
It's a pain to have to code constructors for objects and especially when you add more properties to an object, in this case generally in the past you'd create a new constructor or modify an existing one. You needn't do this in C# 3.0 anymore as in order to set properties when we instantiate the object, we can simply set the public properties on the same line as we instantiate the object - Much like calling a constructor but without having to write any code! This is where the power of object intializers come into play with C# 3.0. Consider the following code:
//C# 3.0In reference to the above code, in C# 2.0 there is no way of setting the objects properties at initialization time without having to call the properties explicitly after we have a reference to the object. Well in C# 3.0 now we can. This is how we do it in C# 3.0:
public class Person
{
private string name = string.Empty;
private int age = -1;
private bool married = false;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public bool Married
{
get
{
return married;
}
set
{
married = value;
}
}
public int Age
{
get
{
return age;
}
set
{
age = value;
}
}
}
Person person = new PersonYou could also use the above with Implicitly Typed Local Variables which is another new feature of C# 3.0. I've written an article on this here.
{
Name = "Simon",
Age = 30,
Married = true
};
So if I wanted to use the above with an Implicitly Typed Local Variables I could code the above as:
var person = new PersonNotice the syntax is a little like Anonymous Types.
{
Name = "Simon",
Age = 30,
Married = true
};
Intellisense is quite clever also, the compiler and intellisense will not allow you to define a property more than once, if you do, in this case defining name twice you will get a compiler error: "Duplicate initialization of member 'Name'". Once you have set a property, intellisense will omit that property from the list of available properties. Much like overriding methods that we have enjoyed since year dot.
Now that we have covered object initializers, we will talk about collection initializers.
List<Person> peopleCollection = new List<Person>Nice and clean! How much easier is that. I like this new feature of C# as it makes my code simpler which then makes it easier to change and understand.
{
new Person{Name = "Simon Hart", Married=True, Age=30}
};
No comments:
Post a Comment