views:

135

answers:

2

I've been watching some videos on asp.net MVC, I think this question is just a general C# one though.

I notice sometimes in the example code, method paramters are called like this

blah.method("something here",
            "Something else",
            new { blah=item.someProperty });

Can you explain what is happening for the 3rd parameter? As I see from the video I am watching, the method takes in an object for the third paramter.

+6  A: 

It is an anonymous type, with a property called blah, given the value from evaluating item.someProperty. In some cases it is an interesting way of passing a set of key/value pairs - easier than a dictionary, for example:

new {Forename="Fred", Surname="Jones" }

is simpler than the same with a dictionary and two records ("Forename"/"Fred", "Surname"/"Jones"). MVC uses this approach in a number of places to pass in semi-optional parameters (a bit like convention over configuration). It is also quite comparable to a lot of jQuery, and let's face it: most MVC users are also using jQuery, so the two approaches site quite nicely together.

I discuss this topic (along with a string.Format example from SO) on my blog, here: Pass data simply; learning from jQuery and ASP.NET MVC.

Marc Gravell
I kind of understand, but why do this instead of new object [] { myObject (, object2, object3 etc.) } ?
SLC
ActionLink is actually what I saw it being used in, heh.
SLC
Because the **names** are important in the usage too; not just the values.
Marc Gravell
In actionlink, it is saying the **action** is `<x>`, the **id** is `<y>` etc - if your action needs a `tickleCount` parameter, you can add `, tickleCount = 27` (or whatever).
Marc Gravell
I think I understand now, thanks!
SLC
+4  A: 

The third object is an anonymous type. It is essentially a type that has no name, that the compiler creates for you to allow you to pass or manipulate information without requiring the developer to create a specific type for it.

You see this practice quite a bit in code that uses LINQ, where anonymous types are used to simplify the process of creating projections.

Anonymous types are created using a syntax like:

var anonType = new { Age = 25, Color = "Red", ... };

You can only assign anonymous types to variables of type var (or object). Any anonymous types that have the same field names of the same type are considered identical by the compiler.

Anonymous types also define some reasonable equality and hashing semantics for you, so they can be compared and used as dictionary keys.

Anonymous types can be superior to object[] or Tuple. First, they are strongly typed - which means you can't accidentally mix incompatible anonymous types. Second, they allow tools like VisualStudio to provide intellisense. Third, they automatically generate comparison and hashing semantics for you, as mentioned above. Finally, unlike Tuple<>, you can define an anonymous type with any number of fields - which is impractical with Tuple.

LBushkin