×

C# Passing array to function

We can pass array to function by using its name only. The formal parameter in function declaration must be of array type. Function accepts arrays to implement any logic on them which can be reused. Let’s see an example of passing array to the function.
using System;  
public class ArrayExample  
{  
static void printArray(int[] arr)  
{  
Console.WriteLine("Printing array elements:");  
for (int i = 0; i < arr.Length; i++)  
{  
Console.WriteLine(arr[i]);  
}  
}  
public static void Main(string[] args)  
{  
int[] arr1 = { 25, 10, 20, 15, 40, 50 };  
int[] arr2 = { 12, 23, 44, 11, 54 };  
printArray(arr1);
            printArray(arr2);  
}  
}
Output
Printing array elements:
25
10
20
15
40
50
Printing array elements:
12
23
44
11
54
Passing Array to function: finding second largest element of array
using System;  
public class ArrayExample  
{  
public static void Main(string[] args)
            {
                        int[] arr=new int[10];
                        for(int i=0;i<arr.Length;i++)
                        {
                                    arr[i]=Convert.ToInt32(Console.ReadLine());
                        }
                        int m=secondLargest(arr);
                        Console.WriteLine("Second largest element in the array is "+m);
            }
            static int secondLargest(int[] arr)
            {
                        int largest=arr[0];
                        int second_largest=arr[0];
                        for(int i=0;i<arr.Length;i++)
                        {
                                    if(arr[i]>largest)
                                    {
                                                largest=arr[i];
                                    }
                        }
                        for(int i=1;i<arr.Length;i++)
                        {
                                    if(arr[i]>second_largest && arr[i]<largest)
                                                second_largest=arr[i];
                        }                                          
                        return second_largest;
            }
}
Output
10
20
30
40
50
60
70
80
80
90
Second largest element in the array is 80  
Passing Array to function Example: sorting the array using bubble sort
using System;  
public class ArrayExample  
{  
public static void Main(string[] args)
            {
                        int[] arr=new int[10];
                        for(int i=0;i<arr.Length;i++)
                        {
                                    arr[i]=Convert.ToInt32(Console.ReadLine());
                        }
                        sort(arr);
            }
            static void sort(int[] arr)
            {
                        int temp;
                        Console.WriteLine("The Sorted array is... ");
                        for(int i=0;i<arr.Length;i++)
                        {
                                    for(int j=i+1;j<arr.Length;j++)
                                    {
                                                if(arr[j]<arr[i])
                                                {
                                                            temp=arr[i];
                                                            arr[i]=arr[j];
                                                            arr[j]=temp;
                                                }
                                    }
                        }
                        for(int i=0;i<arr.Length;i++)
                        {
                                    Console.WriteLine(arr[i]);
                        }
            }
}
Output
10
12
3
2
6
1
7
3
2
1
The Sorted array is... 
1
1
2
2
3
3
6
7
10
12
Passing Array to function: printing sum and average of all elements
using System;  
public class ArrayExample  
{  
public static void Main(string[] args)
            {
                        int[] arr=new int[5];
                        for(int i=0;i<arr.Length;i++)
                        {
                                    arr[i]=Convert.ToInt32(Console.ReadLine());
                        }
                        sum(arr);
            }
            static void sum(int[] arr)
            {
                        int sum=0;
                        float avg=0;
                        for(int i=0;i<arr.Length;i++)
                        {
                                    sum=sum+arr[i];
                        }
                        avg=sum/5;
                        Console.WriteLine("sum of the array is "+sum+" Average of the array is "+avg);
            }
}
Output
1
2
3
4
5
sum of the array is 15 Average of the array is 3

Related Topics

C# Data Types

A Data type is the type of data which the variable can store such as integer, float, double, character, etc. In C#, Data types are divided into three parts 1. Value data type 2....

2 minutes read.

C# functions with out variable

In C#, the out variable can be used like a reference variable. We don't need to initialize the variable before passing it into a function call as a out-type. Using...

1 minute read.

C# Base

C# provides a base keyword to access base class properties in the chilled class. C# Base is almost similar to the super in java. It can only be used inside...

3 minutes read.

C# First Application

Let's see the first simple program in C# which prints Hello word on the console. class FirstExample { static void Main (string[] args) { System.Console.WriteLine("Hello World "); } } Output Hello World Description of the first Program: class: it’s...

2 minutes read.

C# Method Overriding

Method overriding is the mechanism where derived class defines the same method as parent class with more functionality in the chilled class. It provides runtime polymorphism in C#. It provides...

1 minute read.

C# Abstraction

Abstraction is a mechanism by which we can hide the complexities and show only functionalities. C# provides an abstract keyword to declare a class and method as abstract. In C#,...

2 minutes read.

C# Polymorphism

Polymorphism is the combination of two words, poly+forms which means many forms. It is a greek word . In C#, Polymorphism is achieved by mixing three mechanisms that are Inheritance,...

3 minutes read.

C# Member overloading

If we define two members in a class with the same name then it's called member overloading. Advantage of member overloading is the thing that we don't need to give...

4 minutes read.

C# Constructors

In C#, Constructor is the special method with no return type. It is invoked automatically at the time of object creation. Its name must be the same as the class...

2 minutes read.

C# Destructor

Destructors works in the opposite way. They basically destruct the objects. We can’t send parameter in a destructor. We also can’t apply any modifier to the destructor. Likewise constructor, destructor...

1 minute read.

C# Aggregation

In C#, Aggregation Represents Has-A Relationship between two objects. A field of the class defines another class to reuse this in the form of association. The class is used as...

3 minutes read.

C# Array Class

In C#, Array class deals with all array related operations. It provides methods for various purposes such as for sorting, searching, minimum, maximum, and many more. This class works as...

5 minutes read.

C# Operators

Operator is a symbol that is responsible for the operations. There can be variety of operators for example arithmetic, logical, bitwise and many more. There are following type of operators used...

1 minute read.

C# enum

Enum in C# is also known as enumerations. It basically refers to a named set of constants such as months, days, seasons, etc. Enum improves type safety. They contain fixed set...

3 minutes read.

C# If Statements

In C#, If Statements are used to check the condition. Expression given in if statement is evaluated, If it is true then if block gets executed if it is false...

2 minutes read.

C# Tutorial

The C# Tutorial provides basic and advanced knowledge of C# . This will help the beginners as well as professionals in learning just more then enough about C#, A Programming...

3 minutes read.

C# this Keyword

In C#, this is a keyword used to refer current class instance. It is mainly used to distinguish instance variables of the class and formal parameters defined in a constructor...

7 minutes read.

C# Sealed

C# provides sealed keyword to apply restrictions on class and methods. If a class is defined as sealed then it can’t be inherited. If a method is defined as sealed...

3 minutes read.

Ref and Out in C#

 Ref in c# The ref is keyword that describes that any value that passes through the reference. We can use the ref keyword in 4 ways; In a programming code, in that...

4 minutes read.

C# Multidimensional Arrays

In C#, A multidimensional Array represents data storage in tabular form (in the form of matrix (rows and columns)). Number of dimensions is not fixed. It can be 2, 3...

1 minute read.