O'Reilly logo

C# Data Structures and Algorithms by Marcin Jamro

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required