2

Using Asp.Net-MVC music store tutorial, concrete page is at :

http://www.asp.net/mvc/tutorials/mvc-music-store-part-3

<ul>    
    @foreach (var genre in Model)  
    {  
        <li>
            @Html.ActionLink(genre.Name,"Browse", new { genre = genre.Name })
        </li>  
    }  
</ul>    

Variable genre is reference for Object created in Controller Class which has one property - Name, and i can't menage to understand this new{genre=genre.Name} instantiation-assignment. Can someone give me a link or explanation of this language feature?

2 Answers 2

5

It is an object initializer, creating an anonymous type.

In this case, the anonymous type has a genre string property, initialized to the enclosing genre.Name property.

The naming in this code sample is a bit poor and can be confusing.

Sign up to request clarification or add additional context in comments.

2 Comments

Thx man, renamed that property of anonymous type to be location_id for now. Cool, so when it comes to anonymous types i can define them without following declaration and use them as some kind of short life helper types. Do correct me if wrong.
@streetspirit - short lived types is indeed one of the use cases for anonymous types (very useful in LINQ).
2

That looks like Anonymous type.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.