Paulo Morgado

.NET Development & Architecture

This Blog

Syndication

Search

Tags

News

Unit Test Today! Get Typemock Isolator!

Projects

Books

 

Visitors

Visitor Locations

Community

Email Notifications

Archives

Profile

Disclaimer

The opinions and viewpoints expressed in this site are mine and do not necessarily reflect those of Microsoft, my employer or any community that I belong to. Any code or opinions are offered as is. Products or services mentioned are purchased by me, made available to me by my employer or the manufacturer/vendor which doesn't influence my opinion in any way.

How About Property Assignment And Collection Adding Like Object And Collection Initializers In C#?

C# 3.0 introduced object and collection initializers. It is now easier to initialize objects or collections:

var person = new Person { FirstName = "Paulo", LastName = "Morgado" };

var persons = new List<Person> {
    new Person { FirstName = "Paulo", LastName = "Morgado" },
    new Person { FirstName = "Luís", LastName = "Abreu" }
};

var personDirectory = new Dictionary<string, Person> {
    { "Lisboa", new Person { FirstName = "Paulo", LastName = "Morgado" } },
    { "Funchal", new Person { FirstName = "Luís", LastName = "Abreu" } }
};

Wouldn't be nice to be able to do the same on already created objects and collections?

But, what would the syntax used be? Something like this?

var person = new Person();
person = { FirstName = "Paulo", LastName = "Morgado" };

var persons = new List<Person>();
persons += {
    new Person { FirstName = "Paulo", LastName = "Morgado" },
    new Person { FirstName = "Luís", LastName = "Abreu" }
};

var personDirectory = new Dictionary<string, Person>();
personDirectory += {
    { "Lisboa", new Person { FirstName = "Paulo", LastName = "Morgado" } },
    { "Funchal", new Person { FirstName = "Luís", LastName = "Abreu" } }
};

What do you think of this?

Published Mon, Aug 11 2008 0:27 by Paulo Morgado

Filed under: , , , ,

Comments

# Good idea@ Monday, August 11, 2008 9:44 AM

I think, you can implement this using extension methods for generic List and Dictionary classes.

e.g.

public static class Extensions {

public static List<TItem> operator +(this List<TItem> list, List<TItem> anotherList)

{...}

public static List<TItem> operator +(this List<TItem> list, TType item)

{...}

public static Dictionary<TKey, TValue> operator +(this IDictionary<TKey, TValue> dictionary, IDictionary<TKey, TValue> anotherDictionary)

{...}

public static Dictionary<TKey, TValue> operator +(this Dictionary<TKey, TValue> list, DictionaryEntry<TKey, TValue>)

{...}

}

Ruslan Urban

# Good idea@ Monday, August 11, 2008 10:44 AM

I think, it can be done using extension methods. E.g.

public static class Extensions {

public static List<TItem> operator +(this List<TItem> list, List<TItem> anotherList)

{...}

public static List<TItem> operator +(this List<TItem> list, TType item)

{...}

public static Dictionary<TKey, TValue> operator +(this IDictionary<TKey, TValue> dictionary, IDictionary<TKey, TValue> anotherDictionary)

{...}

public static Dictionary<TKey, TValue> operator +(this Dictionary<TKey, TValue> list, DictionaryEntry<TKey, TValue>)

{...}

}

Ruslan Urban

# re: How About Property Assignment And Collection Adding Like Object And Collection Initializers In C#?@ Monday, August 11, 2008 5:22 PM

At this point, you cannot add extension operators.

Paulo Morgado

# re: How About Property Assignment And Collection Adding Like Object And Collection Initializers In C#?@ Thursday, September 11, 2008 7:07 PM

I think it would be great.  I constantly find myself cussing because you can't do it.

Brian Johnston

# re: How About Property Assignment And Collection Adding Like Object And Collection Initializers In C#?@ Thursday, September 11, 2008 7:49 PM

One can always dream.

Paulo Morgado

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above: