Example – month names

To summarize the information you have learned about single-dimensional arrays, let's take a look at a simple example, where the array is used to store names of months in English. Such names should be obtained automatically, not by hardcoding them in the code.

The implementation is shown here:

string[] months = new string[12]; 
 
for (int month = 1; month <= 12; month++) 
{ 
    DateTime firstDay = new DateTime(DateTime.Now.Year, month, 1); 
    string name = firstDay.ToString("MMMM",  
        CultureInfo.CreateSpecificCulture("en")); 
    months[month - 1] = name; 
} 
 
foreach (string month in months) 
{ 
    Console.WriteLine($"-> {month}"); 
} 

At the start, a new single-dimensional array is declared and initialized with default values. It contains 12 ...

Get C# Data Structures and Algorithms now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.