C# Example: Creating an array, initializing its values, and performing operations on it - Biz Tech

C# Example: Creating an array, initializing its values, and performing operations on it

Listen
using System;

class Program
{
    static void Main(string[] args)
    {
        int[] numbers = new int[5];
        numbers[0] = 1;
        numbers[1] = 3;
        numbers[2] = 5;
        numbers[3] = 7;
        numbers[4] = 9;

        int sum = 0;
        foreach (int number in numbers)
        {
            sum += number;
        }

        int max = int.MinValue;
        for (int i = 0; i < numbers.Length; i++)
        {
            if (numbers[i] > max)
            {
                max = numbers[i];
            }
        }

        Console.WriteLine("Array: " + string.Join(", ", numbers));
        Console.WriteLine("Sum: " + sum);
        Console.WriteLine("Max: " + max);
    }
}

This example creates an array of int values, initializes its values, and performs operations on it. The following steps are performed:

  1. Define an array of int values with five elements using new int[5].
  2. Set the values of the array using the index operator ([]) and assignment statements.
  3. Calculate the sum of the values using a foreach loop.
  4. Find the maximum value of the array using a for loop and a comparison operation.
  5. Output the array, sum, and maximum value to the console using Console.WriteLine().

Note that there are many other operations you can perform on arrays, such as sorting, searching, and filtering. This example is just one possible multi-step process for working with arrays.