get sum of array inner elements c# code example
Example 1: c# sum of array elements#
int[] arr = new int[] { 1, 2, 3 };
int sum = 0;
for (int i = 0; i < arr.Length; i++)
{
sum += arr[i];
}
int[] arr = { 3, 6, 4, 1, 6, 8 };
Array.IndexOf(arr, 6);
int maxValue = anArray.Max();
int maxIndex = anArray.ToList().IndexOf(maxValue);
Example 2: in another method display sum of elements in different arrays in c#
using System;
public class funcexer5
{
public static int Sum(int[] arr1)
{
int tot=0;
for (int i = 0;i < arr1.Length; i++)
tot += arr1[i];
return tot;
}
public static void Main()
{
int[] arr1 = [5, 5, 6, 4];
Console.Write("\n\nFunction : Calculate the sum of the elements in an array :\n");
Console.Write("--------------------------------------------------------------\n");
Console.WriteLine("The sum of the elements of the array is {0}", Sum(arr1));
}
}