Lesson 8.3

Pass Array as Parameter in C# with Programming Example


Objective

How to pass array as parameter in c#
Programming Example

How to pass array as parameter in C#?

Several times you need to pass array as parameter in c sharp. It is as easy as passing other value as parameter. The method will process the array and return output.

For example, you frequently need to find an element in array which is greater than 10. So you don’t need to write program each time for doing this process. You can make a method for this purpose which will accept array as parameter and return output for those number.

Programming Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Pass_Array_Parameter
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = { 3, 6, 56, 12, 37, 83, 11 };
            int max = LargeNumber(arr);
            Console.WriteLine("Large Number = {0}", max);
            Console.ReadKey();
        }

        public static int LargeNumber(int[] arr)
        {
            int size = arr.Length;
            int number = 0;
            for (int i = 0; i < size; i++)
            {
                if (number < arr[i])
                {
                    number = arr[i];
                }
            }
            return number;
        }
    }
}



Output

Large Number = 83
__

Summary

In this chapter you have learned how to pass array as parameter. It is the same like passing other value as parameter. In the next chapter you will learn Params Array in C#.

BackNext