Click here to Skip to main content
15,896,426 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All. i want Swap two value type like int but i can't understand difference between these two method. that on uses from 'Ref' keyword but second doesn't use ref
so What is different between methods?
1. Swap method with ref keyword

C#
static void Main()
        {
            int i, j;
            i = 1;
            j = 2;
            Console.WriteLine(" Befor Swap:\n" + "i:{0}\tj:{1}", i, j);
            Swap(ref i, ref j);
            Console.WriteLine(" After Swap:\n" + "i:{0}\tj:{1}", i, j);
            Console.ReadLine();
        }
       public static void Swap(ref int a, ref int b)
        {
            int t;
            t = a;
            a = b;
            b = t;
        }

2.Swap Method without ref keyword

C#
static void Main()
       {
           int i, j;
           i = 1;
           j = 2;
           Console.WriteLine(" Befor Swap:\n" + "i:{0}\tj:{1}", i, j);
           Swap(i, j);
           Console.WriteLine(" After Swap:\n" + "i:{0}\tj:{1}", i, j);
           Console.ReadLine();
       }
       static void Swap(int a, int b)
       {
           int t;
           t = a;
           a = b;
           b = t;
       }

::: So . Why do we use ref keyword in this case??
Posted

1. Swap method with ref keyword :
This approch will work.
Quote:
The ref keyword causes an argument to be passed by reference, not by value. The effect of passing by reference is that any change to the parameter in the method is reflected in the underlying argument variable in the calling method. The value of a reference parameter is always the same as the value of the underlying argument variable.


2.Swap Method without ref keyword :
This approch will not work because you are passing 'i' & 'j' by value not reference. so it will swap the local variable in the method i.e. 'a' & 'b' not 'i' & 'j'.

for more info value vs ref[^] & http://stackoverflow.com/a/2128669[^]
 
Share this answer
 
v2
Hi,

In above code if you are not using Ref i don't think your code will work. actually you need to either return some value from the function or you need to use ref to use the same variable in the called method. If you swap two values but you do not return it then what does it do in the main program ?(Nothing)

So either you return value/use static variable/use ref or out to return the calculated new values from the swap method.

hope this information is enough to understand.

Thanks.
 
Share this answer
 
Comments
AlirezaDehqani 16-Oct-12 2:20am    
hi AmitGajjar. either methods works correctly with same answers. but i don't understand difference between use of ref keyword and without that version.
AmitGajjar 16-Oct-12 2:30am    
are you sure, can you double check with different value. i will check it when i will get some time.
AmitGajjar 16-Oct-12 2:35am    
I have just test your method without ref and it is not working. 1st example with ref is working as expected.
AlirezaDehqani 16-Oct-12 2:50am    
oh yes. you'r right. i was wrong. i mean this way is working. am i right?:
int i,j;
int t;
i = 1; j=2;

t = i;
i = j;
j = t;
AmitGajjar 16-Oct-12 2:52am    
yes reference variable is required to return multiple values from function. if you do not have function then this code will work fine.
 
Share this answer
 
Comments
AlirezaDehqani 16-Oct-12 2:30am    
yes. you'r right. but i know ref keyword. but i mean in this case if we don't use of ref the swap method returns same answer with without ref version of that

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900