Introduction:

In this tutorial we will learn What is Ref parameter in c# or How to use Ref parameter in C# programming Or Why use Ref parameter in C# With example.


Description:

In c# Ref  keywords are used to pass an parameter as a reference, meaning that when the value of that parameter changes after being passed through the method, the new value is reflected in the calling method. A parameter passed using the ref keyword must be defined in the calling method before getting passed to the called method.

For example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Ref_parameter
{
    class Program
    {
        static void value(ref int val)
        {
            val = 25;
        }
        static void Main(string[] args)
        {
            int val=10;
            value(ref val);
            Console.WriteLine(val);
            Console.Read();
        }
    }
}

Output: 25

Advantages: 

o    Returning values by reference can improve performance.

o    This features helps developers to continue to use safe code, while avoiding unnecessary copies.


I hope this article will help to you.


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

You may also like