ref keyword in c#.net | how to pass reference of the variable to other method using C# with ref keyword

ref keyword in c#.net

Ref keyword is useful for passing the reference address of the variable to other method. Before passing we must initialize that variable.

out is also a keyword in c#.net which is having bit different feature. We can see out keyword here.

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

namespace OOPSConcepts
{
    class Program
    {
        static void Main(string[] args)
        {
            //variable should be initialized before using with ref keyword.    
            int SeconeNumber=0;
            int result = 0;
            result = Add(ref SeconeNumber);
            Console.WriteLine(result); //40
            Console.WriteLine(SeconeNumber); //20
            Console.Read();

        }
        public static int Add(ref int secondNumber)
        {
           // we can assign any other value to variable
            secondNumber = 20;
            return secondNumber +20;
        }
    }
}


See more OOPS DOTNET Interview Questions

See more ASP.NET, C#.NET, SQL Server and more Interview Questions


Share this post :

Post a Comment

Please give your valuable feedback on this post. You can submit any ASP.NET article here. We will post that article in this website by your name.

 
Support : Ranga Rajesh Kumar
Copyright © 2012. ASP.NET Examples - All Rights Reserved
Site Designed by Ranga Rajesh Kumar