C# Passing Array to Function

In C#, you can pass an array to a function by specifying the array type as the parameter type. Here’s an example of how you can pass an array to a function in C#:

class Program
{
    static void Main(string[] args)
    {
        int[] numbers = { 1, 2, 3, 4, 5 };
        
        // Call the function and pass the array
        PrintArray(numbers);
    }

    static void PrintArray(int[] array)
    {
        // Iterate over the array elements and print them
        foreach (int num in array)
        {
            Console.WriteLine(num);
        }
    }
}

In the example above, we define an array of integers called numbers. We then call the PrintArray function and pass the numbers array as an argument. The PrintArray function accepts an array of integers as a parameter (int[] array). Inside the function, we iterate over the elements of the array and print them using the Console.WriteLine method.

When passing an array to a function, keep in mind that arrays are reference types in C#. Therefore, any modifications made to the array inside the function will affect the original array passed from the caller.

C# Passing Array to Function Example: print array elements

Certainly! Here’s an example of a C# program that demonstrates how to pass an array to a function and print its elements:

using System;

class Program
{
    static void Main(string[] args)
    {
        int[] numbers = { 1, 2, 3, 4, 5 };

        // Call the function and pass the array
        PrintArrayElements(numbers);
    }

    static void PrintArrayElements(int[] array)
    {
        // Iterate over the array elements and print them
        for (int i = 0; i < array.Length; i++)
        {
            Console.WriteLine(array[i]);
        }
    }
}

In the above example, we have an array of integers called numbers. We pass this array to the PrintArrayElements function. The function accepts an integer array as a parameter (int[] array). Inside the function, we use a for loop to iterate over the elements of the array and print each element using Console.WriteLine.

When you run this program, it will output the elements of the numbers array:

1
2
3
4
5

Note that we use the array.Length property in the for loop condition to ensure we iterate over all the elements of the array, regardless of its size.

C# Passing Array to Function Example: Print minimum number

Certainly! Here’s an example of a C# program that demonstrates how to pass an array to a function and find the minimum number:

using System;

class Program
{
    static void Main(string[] args)
    {
        int[] numbers = { 9, 5, 2, 7, 1 };

        // Call the function and pass the array
        int minimumNumber = FindMinimum(numbers);

        Console.WriteLine("The minimum number is: " + minimumNumber);
    }

    static int FindMinimum(int[] array)
    {
        int minimum = array[0];

        for (int i = 1; i < array.Length; i++)
        {
            if (array[i] < minimum)
            {
                minimum = array[i];
            }
        }

        return minimum;
    }
}

In this example, we have an array of integers called numbers. We pass this array to the FindMinimum function. The function accepts an integer array as a parameter (int[] array). Inside the function, we initialize a variable minimum with the first element of the array. Then, we iterate over the remaining elements of the array using a for loop. If we find an element that is smaller than the current minimum, we update the value of minimum to the new smaller value. Finally, we return the minimum value from the function.

When you run this program, it will output the minimum number from the numbers array:

The minimum number is: 1

Note that we assume the array has at least one element in this example. You may want to add additional checks if the array can be empty to handle such cases appropriately.

C# Passing Array to Function Example: Print maximum number

Certainly! Here’s an example of a C# program that demonstrates how to pass an array to a function and find the maximum number:

using System;

class Program
{
    static void Main(string[] args)
    {
        int[] numbers = { 9, 5, 2, 7, 1 };

        // Call the function and pass the array
        int maximumNumber = FindMaximum(numbers);

        Console.WriteLine("The maximum number is: " + maximumNumber);
    }

    static int FindMaximum(int[] array)
    {
        int maximum = array[0];

        for (int i = 1; i < array.Length; i++)
        {
            if (array[i] > maximum)
            {
                maximum = array[i];
            }
        }

        return maximum;
    }
}

In this example, we have an array of integers called numbers. We pass this array to the FindMaximum function. The function accepts an integer array as a parameter (int[] array). Inside the function, we initialize a variable maximum with the first element of the array. Then, we iterate over the remaining elements of the array using a for loop. If we find an element that is larger than the current maximum, we update the value of maximum to the new larger value. Finally, we return the maximum value from the function.

When you run this program, it will output the maximum number from the numbers array:

The maximum number is: 9

Note that we assume the array has at least one element in this example. You may want to add additional checks if the array can be empty to handle such cases appropriately.