交换带引用和不带引用的值类型? [英] Swap Value Types with ref and without ref?!!!!

查看:121
本文介绍了交换带引用和不带引用的值类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


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


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??

推荐答案

1.带有ref关键字的交换方法:
此方法将起作用.
1. Swap method with ref keyword :
This approch will work.
报价:

ref关键字使参数按引用而不是按值传递.通过引用传递的效果是,对方法中的参数所做的任何更改都会反映在调用方法中的基础参数变量中.引用参数的值始终与基础参数变量的值相同.

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.不带ref关键字的交换方法:
由于您传递的是"i"& ;,此方法无法正常工作值"j"不参考.因此它将在方法中交换局部变量,即"a"和"b"不是"i"& ``j''.

有关更多信息,请值与参考值 [ ^ ]& http://stackoverflow.com/a/2128669 [



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[^]




在上面的代码中,如果您不使用Ref,我认为您的代码将无法工作.实际上,您需要从函数返回一些值,或者需要使用ref在调用的方法中使用相同的变量.如果交换两个值但不返回它,那么它在主程序中有什么作用?(无)

因此,您可以返回值/使用静态变量/使用ref或out来从swap方法返回计算出的新值.

希望这些信息足以理解.

谢谢.
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.


使用"ref"之前先了解它-
Understand ''ref'' before using it - http://msdn.microsoft.com/en-us/library/14akc2c7(v=vs.71).aspx[^]


这篇关于交换带引用和不带引用的值类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆