C# Out Parameter

C# out parameter is like type of parameter that is declared with using out keyword. It is the like as reference parameter, that doesn’t create memory allocation.

Usually, a method returns value with using return keyword. Unfortunately, a return modifier can return only one value at a time. Sometime, your C# program required to return multiple values from a single method. In this situation, you need like type of function that can produce multiple output result from a single function. The output parameter C# lets your program to return multiple values

Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Out_Parameter_Example
{
    class Program
    {
        //Accept two input parameter and returns two out value
        public static void Rect(int len, int width, out int area, out int perimeter)
        {
            area = len * width;
            perimeter = 2 * (len + width);
        }
        static void Main(string[] args)
        {

            int area, perimeter;
            // passing two parameter and getting two returning value
            Program.Rect(5, 4, out area, out perimeter);
            Console.WriteLine("Area of Rectangle is {0}\t", area);
            Console.WriteLine("Perimeter of Rectangle is {0}\t", perimeter);
            Console.ReadLine();
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *