Skip to main content

Passing array as parameter in C#

An array can also be passed to method as argument or parameter. A method process the array and returns output. Passing array as parameter in C# is pretty easy as passing other value as parameter. Just create a function that accepts array as argument and then process them. The following demonstration will help you to understand how to pass array as argument in C# programming.
Programming Example of passing array as parameter in C#


using System;
 
namespace array_parameter
{
  class Program
   {
     static void printarray(int[] newarray)
      {
        int i,sum=0;
        Console.Write("\n\nYou entered:\t");
        for (i = 0; i < 4; i++)
         {
           Console.Write("{0}\t", newarray[i]);
           sum = sum + newarray[i];
         }
        Console.Write("\n\nAnd sum of all value is:                 \t{0}", sum);
        Console.ReadLine();
      }
     static void Main(string[] args)
      {
        int[] arr = new int[4];
        int i;
        for (i = 0; i < 4; i++)
         {
           Console.Write("Enter number:\t");
           arr[i] = Convert.ToInt32(Console.ReadLine());
         }
        // passing array as argument
        Program.printarray(arr);
      }
   }
}







Output

 

Enter number:      5
Enter number:      3
Enter number:      2
Enter number:      1


You entered:     5    3    2    1

Add sum of all value is:       11

  • In the preceding example, we create an array and accept some integer value from the user at runtime. Then we passed array as argument to printarray(int[] newarray)  for printing and other calculation. It is same as other value passed as parameter to the function.

Comments

Popular posts from this blog

C# Array

Array is a collection of variable of same data type. If you have declare 1000 integer variable, then you can declare an integer type array of 1000 size. The value of array can be accessed using index position of array. The first index position of array is zero. In C#, there two types of array: Single Dimensional Array and Multi Dimensional Array. You can use both type of array easily and can access its element using loop constructs or index position.

Structure (C#)

Structure is the value type data type that can contain variables, methods, properties, events and so on. It simplifies the program and enhance performance of code in C# programming. The structure encapsulate small group of related variables inside a single user-defined data type. It improves speed and memory usage and also enhances performance and clarity of your code. How to use structure in C#? It is very simple to use structure in C#. The following programming example will show you to how to create and use structure in C# programming. Programming Example of Structure (C#) using System; namespace Structure {    class Program    {      // creating three different variable in single structure       struct book        {          public string bookname;          public int price;          public stri...