Index Initializers (C# Only)

Prior language editions brought developers the concept of creating an object and initializing its values at the same time. (See “Object Initializers” later in this chapter.) However, you could not initialize objects that used indexes. Instead, you had to add one value after another, making your code repetitive and hard to read. C# 6.0 supports index initializers. The following shows an example of creating a Dictionary<string, DateTime> object of key/value pairs and initializing values at the same time.

C#

var holidays = new Dictionary<string, DateTime>
{
    { "New Years", new DateTime(2015, 1, 1) },
    { "Independence Day", new DateTime(2015, 7, 4) }
};

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset