c# - How to get the average of an int array elements

Int array average
The following asp.net c# example code demonstrate us how can we get average from an int array elements value programmatically in an asp.net application. Int array represent an array of elements whose all values are integer (Int32).

Int array only hold integer data type values, so we can apply number only methods of int array elements/items value. Linq Average() method computes the average of a sequence of numeric values. So we can get average of an int array values.

We can call the Average() method as this way IntArray.Average(). This method has no required o optional parameter to pass, we just need to call the method after specifying a numeric values sequence.

<%@ Page Language="C#" AutoEventWireup="true"%>  
<!DOCTYPE html>  
<script runat="server">  
    protected void Button1_Click(object sender, System.EventArgs e)  
        int[] exammarks = new int[] { 99,55,74,66,69};
        Label1.Text = "marks array.......<br/>";
        foreach (int i in exammarks)
            Label1.Text += i.ToString() + "<br/>";

        double average = exammarks.Average();
        Label1.Text += "<br />Average Marks: " + average.ToString();
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>c# example - int array average</title>  
    <form id="form1" runat="server">  
        <h2 style="color:DarkBlue; font-style:italic;">  
            c# example - int array average
        <hr width="550" align="left" color="LightBlue" />    
        <br /><br />
            Text="int array average"  
More c# examples