HI WELCOME TO SIRIS

C# Passing Array to Function

Leave a Comment
In C#, to reuse the array logic, we can create function. To pass array to function in C#, we need to provide only array name.
  1. functionname(arrayname);//passing array  

C# Passing Array to Function Example: print array elements

Let's see an example of C# function which prints the array elements.
  1. using System;  
  2. public class ArrayExample  
  3. {  
  4.     static void printArray(int[] arr)  
  5.     {  
  6.         Console.WriteLine("Printing array elements:");  
  7.         for (int i = 0; i < arr.Length; i++)  
  8.         {  
  9.               Console.WriteLine(arr[i]);  
  10.         }  
  11.     }  
  12.     public static void Main(string[] args)  
  13.     {  
  14.         int[] arr1 = { 25, 10, 20, 15, 40, 50 };  
  15.         int[] arr2 = { 12, 23, 44, 11, 54 };  
  16.         printArray(arr1);//passing array to function  
  17.         printArray(arr2);  
  18.     }  
  19. }  
Output:
Printing array elements:
25
10
20
15
40
50
Printing array elements:
12
23
44
11
54

C# Passing Array to Function Example: Print minimum number

Let's see an example of C# array which prints minimum number in an array using function.
  1. using System;  
  2. public class ArrayExample  
  3. {  
  4.     static void printMin(int[] arr)  
  5.     {  
  6.         int min = arr[0];  
  7.         for (int i = 1; i < arr.Length; i++)  
  8.         {  
  9.             if (min > arr[i])  
  10.             {  
  11.                 min = arr[i];  
  12.             }  
  13.         }  
  14.         Console.WriteLine("Minimum element is: " + min);  
  15.     }  
  16.     public static void Main(string[] args)  
  17.     {  
  18.         int[] arr1 = { 25, 10, 20, 15, 40, 50 };  
  19.         int[] arr2 = { 12, 23, 44, 11, 54 };  
  20.   
  21.         printMin(arr1);//passing array to function  
  22.         printMin(arr2);  
  23.     }  
  24. }  
Output:
Minimum element is: 10
Minimum element is: 11

C# Passing Array to Function Example: Print maximum number

Let's see an example of C# array which prints maximum number in an array using function.
  1. using System;  
  2. public class ArrayExample  
  3. {  
  4.     static void printMax(int[] arr)  
  5.     {  
  6.         int max = arr[0];  
  7.         for (int i = 1; i < arr.Length; i++)  
  8.         {  
  9.             if (max < arr[i])  
  10.             {  
  11.                 max = arr[i];  
  12.             }  
  13.         }  
  14.         Console.WriteLine("Maximum element is: " + max);  
  15.     }  
  16.     public static void Main(string[] args)  
  17.     {  
  18.         int[] arr1 = { 25, 10, 20, 15, 40, 50 };  
  19.         int[] arr2 = { 12, 23, 64, 11, 54 };  
  20.   
  21.         printMax(arr1);//passing array to function  
  22.         printMax(arr2);  
  23.     }  
  24. }  
Output:

Maximum element is: 50
Maximum element is: 64

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.