An array of C#, PHP, and HTML programming articles, tutorials, and resources

Posts Tagged ‘ list ’

Calculating the Average from a List of numbers is accomplished through this easy function. The average is the sum of all values in a list divided by the total number of values. In this average example, we will assume the List consists of doubles (by means of a Generic List). For this to workOur Generic List is defined below:

List<double> myGenericDoubleList = new List<double>();
myGenericDoubleList.Add(5.2);
myGenericDoubleList.Add(8.4);
myGenericDoubleList.Add(435);
myGenericDoubleList.Add(76.9);
myGenericDoubleList.Add(3256.1);
myGenericDoubleList.Add(-85.2);

Now that we have defined our Generic Double List, we need to define our average function. Our function will take in a list of double values (the one we previously defined as myGenericDoubleList) and return a double value (which is our final double value). The average function reads in all double values and adds the value to a running sum. Finally, we divide the running sum by the total number of items in the list.

private double getAverage(List<double> doubleList)
{
    double total = 0;
    foreach (double value in doubleList)
    {
        total += value;
    }
    double average = total / doubleList.Count;
    return average;
}

Now that the function is ready to be used, we simply need to call the average function. An example is provided below:

double finalAverage = getAverage(myGenericDoubleList);

And there you have our complete example. Also important to know is a function has been provided out of the box with .NET Framework 3.5 that calculates the average of a list. To access this function, you be required to have a Generic List of double.

double finalDouble = myGenericDoubleList.Average()

How to Use a Generic List in C#

by Victor | September 10, 2008 in C# | 3 Comments

Using a Generic List in C# is an efficient method of storing a collection of variables. And probably the best part is that the List is strongly typed and casting (which degrades performance) will no longer be necessary. Your collection of variables should be of the same data type. Generic Lists were first introduced into the .NET Framework 2.0. Before we begin our Generic List example, first remember to include the correct namespace below:

using System.Collections.Generic;

In our example, we will store the integers 5, 8, 435, 76, 3256, and -85. Our next step is to initialize a List using Generics. Because we are storing integers, our it with be an int Generics List. If you wish to create the List as another variable type, simply replace int with double, decimal, string, or another type of element. We will give our List the name myGenericIntegerList.

List<int>myGenericIntegerList = new List<int>();

Now that the List has been created and initialized, we easily add integer elements. Since we stated this list as an int, the input will now be strongly typed to only accept integer values. We will now add the integeres5, 8, 435, 76, 3256, and -85 iin the example below:

myGenericIntegerList.Add(5);
myGenericIntegerList.Add(8);
myGenericIntegerList.Add(435);
myGenericIntegerList.Add(76);
myGenericIntegerList.Add(3256);
myGenericIntegerList.Add(-85);

Your generic int List is now complete! Another advantage to using the generics List is there are helper functions such as average, contains, and counts, and toArray. As brought up by Khalid, the average function is an extension method.

double average = myGenericIntegerList.Average();
bool contains = myGenericIntegerList.Contains(4);
int count = myGenericIntegerList.Count;
int[] intArray = myGenericIntegerList.ToArray();

For more functions, you can either visit the MSDN and search for “List(T) Methods” or better just give the function a try in Visual Studio and let Intellisense show you other functions.

Unlike a list of check boxes, a list of radio buttons is designed to allow users to select only one option. However, once an option is selected for a radio button, it is now more difficult to deselect all options. However, a C# function ClearSelection, will allow the selection to be cleared.

In our example below is a RadioButtonList called rblSelectAOption:

<asp:RadioButtonList ID="rblSelectAOption" runat="server">
<asp:ListItem>One</asp:ListItem>
<asp:ListItem>Two</asp:ListItem>
<asp:ListItem>Three</asp:ListItem>
</asp:RadioButtonList>

Next, the codebehind below will clear all selected option:

rblSelectAOption.ClearSelection();

That’s all it takes!