From the course: .NET Essentials: Working with LINQ

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

Anonymous types

Anonymous types

- [Instructor] Anonymous types provide a way to create a class on the fly. And this is useful in Link when you're creating queries and you want to project out a new type as a result of that query. Now, an anonymous type consists of only read-only properties. And you do not define a type ahead of time. The type name is created by the compiler and it uses some special naming convention so that its types will never conflict with any of the types that you might create. It also it says here in line eight, "The type "of each property is inferred by the compiler." We can do that because we can implicitly type now in C#. Now, you use the new operator like I'm doing here on line 17 but then instead of using the type name here, you just leave it blank. And then you do a set of open and closed curly braces. And then you're going to provide the name of the read-only property and the value of the read-only property. And just like…

Contents