From the course: .NET Essentials: Working with LINQ

Unlock this course with a free trial

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

Collection initializers

Collection initializers

- [Instructor] At the same time Microsoft created the object initializer syntax. They added a similar syntax for working with groups of data. For example, a raise and lists and dictionaries. This is called collection initializers and here's how it works. On line three I'm declaring a new list of Int32, and now I want to initialize that new list with some items. So rather than having to do multiple separate lines, remember in functional programming, we don't like separate statements. We'd like to do everything on a single line using method calls, if possible. So that's what we're doing here. So I have the open and closed curly braces. And then I do a comma delimited set of data that is added to the list. In this case, there's four numbers here. So four items will get added to the list. It's similar syntax for this double array. It gets a little more complicated on line five because I'm working with a dictionary that has…

Contents