You need to remember that C# 6.0 does not introduce big new concepts, but small features designed to make your code cleaner and easier to read and understand. With index initializers, this is not an exception. You can now initialize the indices of newly created objects. This means you do not have to use separate statements to initialize the indexes.
The change here is subtle. We will create a method to return the day of the week based on an integer. We will also create a method to return the start of the financial year and salary increase month, and then set the salary increase month to a different value than the default. Finally we will use properties to return a specific type of species to the console window.
Recipe4IndexInitializers
and add a second class called Month
to your code. The Month
class simply contains two auto-implemented properties that have been initialized. StartFinancialYearMonth
has been set to month two (February), and SalaryIncreaseMonth
has been set to month three (March):public static class Recipe4IndexInitializers { } public class Month { public int StartFinancialYearMonth { get; set; } = 2; public int SalaryIncreaseMonth { get; set; } = 3; }
ReturnWeekDay
that takes an integer for the day number as a parameter, to the Recipe4IndexInitializers
class:public static string ReturnWeekDay(int dayNumber) { Dictionary<int, string> day = new Dictionary<int, string> { [1] = "Monday", [2] = "Tuesday", [3] = "Wednesday", [4] = "Thursday", [5] = "Friday", [6] = "Saturday", [7] = "Sunday" }; return day[dayNumber]; }
ReturnFinancialAndBonusMonth
to the Recipe4IndexInitializers
class:public static List<int> ReturnFinancialAndBonusMonth() { Month currentMonth = new Month(); int[] array = new[] { currentMonth.StartFinancialYearMonth, currentMonth.SalaryIncreaseMonth }; return new List<int>(array) { [1] = 2 }; }
DetermineSpecies
to the Recipe4IndexInitializers
class. Your code should look like this:public static string Human { get; set; } = "Homo sapiens"; public static string Sloth { get; set; } = "Choloepus hoffmanni"; public static string Rabbit { get; set; } = "Oryctolagus cuniculus"; public static string Mouse { get; set; } = "Mus musculus"; public static string Hedgehog { get; set; } = "Erinaceus europaeus"; public static string Dolphin { get; set; } = "Tursiops truncatus"; public static string Dog { get; set; } = "Canis lupus familiaris"; public static void DetermineSpecies() { Dictionary<string, string> Species = new Dictionary<string, string> { [Human] = Human + " : Additional species information", [Rabbit] = Rabbit + " : Additional species information", [Sloth] = Sloth + " : Additional species information", [Mouse] = Mouse + " : Additional species information", [Hedgehog] = Hedgehog + " : Additional species information", [Dolphin] = Dolphin + " : Additional species information", [Dog] = Dog + " : Additional species information" }; Console.WriteLine(Species[Human]); }
Recipe4IndexInitializers
class:int DayNumber = 3; string DayOfWeek = Chapter1.Recipe4IndexInitializers.ReturnWeekDay(DayNumber); Console.WriteLine($"Day {DayNumber} is {DayOfWeek}"); List<int> FinancialAndBonusMonth = Chapter1.Recipe4IndexInitializers.ReturnFinancialAndBonusMo nth(); Console.WriteLine("Financial Year Start month and Salary Increase Months are:"); for (int i = 0; i < FinancialAndBonusMonth.Count(); i++) { Console.Write(i == 0 ? FinancialAndBonusMonth[i].ToString() + " and " : FinancialAndBonusMonth[i].ToString()); } Console.WriteLine(); Chapter1.Recipe4IndexInitializers.DetermineSpecies(); Console.Read();
The first method ReturnWeekDay
created a Dictionary<int, string>
object. You can see how the indices are initialized with the day names. If we now pass the day integer to the method, we can return the day name by referencing the index.
In the second example, we called a method called ReturnFinancialAndBonusMonth
that creates an array to hold the financial year start month and the salary increase month. Both properties of the Month
class are initialized to 2
and 3
, respectively. You can see that we are overriding the value of the SalaryIncreaseMonth
property and setting it to 2
. It is done in the following line of code:
return new List<int>(array) { [1] = 2 };
The last example uses the Human
, Rabbit
, Sloth
, Mouse
, Hedgehog
, Dolphin
, and Dog
properties to return the correct index value of the Species
object.