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 – average value

The first example utilizes the generic List class for storing floating point values (of the double type) entered by the user. After typing a number, the average value is calculated and presented in the console. The program stops the operation when an incorrect value is entered by the user.

The code from the Main method in the Program class is as follows:

List<double> numbers = new List<double>(); 
do 
{ 
    Console.Write("Enter the number: "); 
    string numberString = Console.ReadLine(); 
    if (!double.TryParse(numberString, NumberStyles.Float,  
        new NumberFormatInfo(), out double number)) 
    { 
        break; 
    } 
 
    numbers.Add(number); 
    Console.WriteLine($"The average value: {numbers.Average()}"); 
} 
while (true); 

At the beginning, an instance ...

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