sum of elements in different arrays in 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 };

// returns 1
Array.IndexOf(arr, 6);

int maxValue = anArray.Max();
int maxIndex = anArray.ToList().IndexOf(maxValue);

Example 2: sum array c#

//
        // Declare two collections of int elements.
        //
        int[] array1 = { 1, 3, 5, 7 };
        List<int> list1 = new List<int>() { 1, 3, 5, 7 };
        //
        // Use Sum extension on their elements.
        int sum1 = array1.Sum();
        int sum2 = list1.Sum();