---
title: "How to pass arrays using ref and out parameter"  
description: "How to pass arrays using ref and out parameter"  
author: "Anupam Mishra"  
published: 2016-02-09  
updated: 2016-02-09  
canonical: https://www.mindstick.com/forum/33968/how-to-pass-arrays-using-ref-and-out-parameter  
category: "c#"  
tags: ["c#", "asp.net"]  
reading_time: 2 minutes  

---

# How to pass arrays using ref and out parameter

Hi Everyone,I am working on [arrays](https://www.mindstick.com/articles/11955/arrays-and-its-limitations) and i need to pass arrays using [ref](https://www.mindstick.com/interview/217/what-is-the-difference-between-ref-out-parameters) and out [parameter](https://www.mindstick.com/blog/450/parameter-class-in-c-sharp) in a [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server).my function are given below:

```
static void FillArray(ref int[] arr){.......}
```

Please can [anyone give me](https://answers.mindstick.com/qa/41194/can-anyone-give-me-some-high-and-low-points-of-each-of-the-four-first-presidential-administrations) a solution in both cases(ref and out parameter).

## Replies

### Reply by Manoj Bhatt

**ref parameter:** A ref parameter of an array type must be definitely assigned by the caller. Therefore, there is no need to be definitely assigned by the callee. **out parameter:** An out parameter of an array type must be assigned before it is used; that is, it must be assigned by the callee.**Here we demonstrate using your method for both cases:**

```
  // For out parameter    class TestOut    {        static void FillArray(out int[] arr)        {            // Initialize the array:            arr = new int[5] { 10,20,30,40,50 };        }       public static void MainE()        {            // Initialization is not required             int[] theArray;             // Pass the array to the callee using out parameter:            FillArray(out theArray);            // Display the array elements:            System.Console.WriteLine("Array elements are:");            for (int i = 0; i < theArray.Length; i++)            {                System.Console.Write(theArray[i] +" ");            }           }    }    // For Ref Parameter    class TestRef    {        static void FillArray(ref int[] arr)        {            // Create the array on demand:            if (arr == null)            {                arr = new int[10];            }            // Fill the array:            arr[0] = 1000;            arr[4] = 5000;        }        static void Main()        {            // Calling Out parameter static method            TestOut.MainE();            // Initialize the array:            int[] theArray = { 10,20,30,40,50 };            // Pass the array using ref:                        FillArray(ref theArray);            // Display the updated array:            System.Console.WriteLine("Array elements are:");            for (int i = 0; i < theArray.Length; i++)            {                System.Console.Write(theArray[i] +" ");            }                       System.Console.ReadKey();        }    }
```


---

Original Source: https://www.mindstick.com/forum/33968/how-to-pass-arrays-using-ref-and-out-parameter

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
