The out Parameter in C#
In this article, I will explain how do you use out parameter in C# applications.
Introduction
The out parameter can be used to return the value in the same variable passed as a parameter of the method. Any changes made to the parameter will be reflected in the variable. It is used both places; when we declare a method and when we call the method.The following example will explain how we use the out parameter in C#.
Example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace use_out
{
class decompose
{
public int Analyse(double n, out double frac)
{
int whole;
whole = (int)n;
frac = n - whole;
return whole;
}
}
class Program
{
static void Main(string[] args)
{
decompose of = new decompose();
int a;
double f;
a = of.Analyse(10.125, out f);
Console.WriteLine("integer part is" + a);
Console.WriteLine("fractional part is" + f);
Console.Read();
}
}
}
In this example we can decomposes a floating-point number into its integer and fractional parts? To do this requires that two pieces of information be returned: the integer portion and the fractional component, this method cannot be written using only a single return value. The Out modifier solves this problem
The output of following program