c# - How to sum all elements of an int array

Sum an int array items
The following asp.net c# example code demonstrate us how can we sum of an int array elements/items value programmatically at run time in an asp.net application. An int array contain elements data type is System.Int32 (Integer), so we can sum them.

Linq Sum() method allow us to sum total values in an array of integers (compute the sum of values in a numeric sequence). We can call the Linq Sum() method as this way Array.Sum() to get summation of array elements value. This method has no required or optional parameter.
sum-an-int-array.aspx

<%@ Page Language="C#" AutoEventWireup="true"%>  
  
<!DOCTYPE html>  
<script runat="server">  
    protected void Button1_Click(object sender, System.EventArgs e)  
    {
        int[] score = new int[] { 125,100,255,500,85};
        
        Label1.Text = "score array.......<br/>";
        foreach (int i in score)
        {
            Label1.Text += i.ToString() + "<br/>";
        }

        int total = score.Sum();
        Label1.Text += "<br />Total Score [Sum]: " + total.ToString();
    }  
</script>  
  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>c# linq example - sum an int array items</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <h2 style="color:DarkBlue; font-style:italic;">  
            c# linq example - sum an int array items
        </h2>  
        <hr width="550" align="left" color="LightBlue" />    
  
        <asp:Label   
            ID="Label1"   
            runat="server"  
            Font-Size="X-Large"  
            >  
        </asp:Label>  
        <br /><br />
        <asp:Button   
            ID="Button1"   
            runat="server"   
            Text="sum an int array items"  
            OnClick="Button1_Click"
            Height="40"  
            Font-Bold="true"  
            />  
    </div>  
    </form>  
</body>  
</html>
More c# examples