November 4, 2011

Ref Keyword(argument passed by reference) Example in C#


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


namespace Ref_Key_Word
{
        class TestRef
        {
            //Pass by Value
            public static void TestWithoutRef(int i)
            {
            i++;
            }


            //Pass by reference
            public static void TestWithRef(ref int i) // note ref
            {
                i++;
            }


            public static void Main()
            {
                //a value will not change after calling a method as is is pass by value
                int a = 2;
                TestWithoutRef(a);
                Console.WriteLine("The value of a is " + a);


                //b value will be chnaged( as it is pass by ref 
                int b = 2;
                TestWithRef(ref b);
                Console.WriteLine("The value of b is " + b);
            }
        }




           
}
Below will be the o/p

No comments:

Post a Comment