Topic : Passing Arrays as Arguments to Method Parameters in C# Programming Language.



Passing Arrays As Arguments:

Arrays can be passed as arguments to Methods Parameters. Methods Process the Array and returns output. Passing Array as parameter in C# is pretty easy as passing other value as parameter. Just Create a Method that accepts array as Arguments and then process them.



Passing Single-Dimensional Arrays as Arguments:

We can Pass any Single-Dimensional Array to a method using this syntax:


//declare and Initializing Arrays;
int[] array = { 1, 2, 3, 4, 5 };
Display(array);   //Calling Display Method:

//Implementation of Display() Method:
void Display(int[] arr)
{
   //Body..
}


Or We can Initialize and Pass a New Array in One step, as shown below.



Display(new int[] { 1, 3, 5, 7, 9 });



Example:

Let's have an simple example to know how exactly we can pass arrays to Methods as an arguments. in this example, we will create an array and input some integers value from user and then we passed the array as argument to Display() Method to show and calculate the Integers.



/*Example: Passing Arrays as an Arguments to Methods: InfoBrother*/

using System;

namespace Arrays_parameter
{
    class Program
    {
        //Display Method: take array as arguments:
        static void Display(int[] newarray)
        {
            int sum = 0;
            Console.Write("\n\nThe Numbers Are:\t");
            for (int i = 0; i < 5; i++) //output
            {
                Console.Write("{0} | ", newarray[i]);
                sum = sum + newarray[i];
            }
            Console.Write("\n\nThe sum of all value is: {0}", sum);
            Console.ReadLine();
        }


        //Main Method:
        static void Main(string[] args)
        {
            int[] arr = new int[5];
            int i;
            for (i = 0; i < 5; i++) //Input:
            {
                Console.Write("Enter number:\t");
                arr[i] = Convert.ToInt32(Console.ReadLine());
            }

            // passing array as argument
            Program.Display(arr); //calling Method:
        }
    }
}


Passing Multi-Dimensional Arrays As Arguments.

Same like one-Dimensional Array, we can pass Multi Dimensional array to a method as an Arguments using this Syntax:


//declare and Initializing Arrays; 
int[,] Array = { { 1, 2 }, { 2, 3 }, { 3, 4 } };
Display(Array);  //calling Methods:

//Implementation of Display Method:
 void Display(int[,] arr)
    {
        // Method code.
    }


Or We can Initialize and Pass a New Array in One step, as shown below.



Display(new int[,] { { 1, 2 }, { 2, 3 }, { 3, 4 }});



Example:

Let's have an simple example to know how exactly we can pass Multi-Dimensional arrays to Methods as an arguments. in This example, We will create a two-Dimensional Array of integers and passed to the Display() Method. The method displays the elements of the Array.



/*Example: Passing Arrays as an Arguments to Methods: InfoBrother*/

using System;

namespace Arrays_parameter
{
    class Program
    {
        static void Display(int[,] arr)  //Method:
        {
            // Display the array elements.
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    System.Console.WriteLine("Element({0},{1})={2}", i, j, arr[i, j]);
                }
            }
        }

        //Main Method:
        static void Main(string[] args)
        {
            // Pass the array as an argument.
            Display(new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } });

            System.Console.ReadKey();
        }
    }
    
}
















I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement