To find the average of a list of numbers in C#, you can use LINQ extension method Average(). Here's an example:
main.cs138 chars4 lines
In this example, we create a list of integers numbers containing 5 elements. We then call the Average() method on this list, which returns the average of all the elements. The result is stored in a double variable average, and printed to the console using Console.WriteLine().
If the list is empty, the Average() method will throw an exception. To avoid this, you can use AverageOrDefault() method instead, which will return 0 (or your chosen default value) for an empty list:
main.cs148 chars4 lines
gistlibby LogSnag