#653 – Projection Initializers

A projection initializer is a declaration for a member of an anonymous type that does not include a property name.

Here’s an example, where the members of the anonymous type are initialized using local variables.  The resulting property names in the anonymously-typed object match the variable names.

string title = "Seven Samurai";
string director = "Akira Kurosawa";
int year = 1956;

var movie = new { title, year, director };

You can also use properties of another object to initialize the anonymously-typed objects members.

            MovieInfo mi = new MovieInfo("Seven Samurai", "Akira Kurosawa", 1956);

            var movie = new { mi.Title, mi.Year, mi.Director };

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: