值类型和引用类型的问题 [英] Value type and reference type problem

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

问题描述

喜 我试图做两objects.My code简单的掉期

Hi I'm trying to do a simple swap of two objects.My code is

void Main()
{
  object First = 5;
  object Second = 10;

  Swap(First, Second);
  //If I display results it displays as 
  //Value of First as 5 and Second as 10
}

private static void Swap(object First, object Second)
{
  object temp = First;
  First = Second;
  Second = temp;
}

由于对象是参考的类型,它的参考应传递给方法,它应该互换。 它为什么没有发生?

Since objects are reference type, its reference should be passed to method and it should swap. why is it not happening?

推荐答案

有各种不同的东西在这里:

There's various different things here:

  • 中的对象的堆住了。忘记他们现在
  • 中的引用的对象的是什么获取存储在首先和第二
  • 您在两个主要的变量
  • 您有两个的参数的在交换
  • the objects live off in the heap. Forget about them for now
  • the references to the objects are what get stored in First and second
  • you have two variables in Main
  • you have two parameters in Swap

现在,最重要的是引用类型/参照之间的区别,并按引用传递。他们的完全无关

now; the important thing is the difference between "reference type / references", and "pass by reference". They are completely unrelated.

在该行:

Swap(First, Second);

您传递两个变量的的的交换。在这种情况下,首先 / 引用盒装对象的。

you pass the values of two variables to Swap. In this case, the value of First / Second is the reference to the boxed object.

下一页;

private static void Swap(object First, object Second)
{
  object temp = First;
  First = Second;
  Second = temp;
}

在这里,你换了两个本地的参数值,但这些都是完全独立于的别的的。如果我们想调用者看到变化的的值的(即重新分配),我们需要传递的引用的:

Here, you swap the value of two local parameters, but these are completely independent to anything else. If we want the caller to see the change to the values (i.e. reassignment), we need to pass by reference:

private static void Swap(ref object First, ref object Second)
{
  object temp = First;
  First = Second;
  Second = temp;
}

现在的首先的价值不再是一个参考盒装对象;这是一个参考的的参考的盒装对象。在来电中,我们使用:

Now the value of First is no longer a reference to the boxed object; it is a reference to a reference to the boxed object. At the caller, we use:

Swap(ref First,ref Second);

这意味着传递的参考变量首先 的,而不是传递变量第一个值 的。

which means pass the reference of variable First, rather than pass the value of variable First.

请注意,我说你可以忘记一个事实,即有一个对象?上述一切都是完全一样的,如果我们使用:

Note that I said you could forget about the fact that there is an object? Everything above is exactly the same if we used:

int x = 1, y = 2;
Swap(ref x, ref y);
void Swap(ref int a, ref int b) {
    var tmp = a; a = b; b = tmp;
}

唯一的区别是,的的 X 为1等,以及 REF X 是引用变量x。在按引用传递,引用类型VS值类型是完全不相干的;唯一重要的事情是理解你在默认情况下,传递变量的的,其中的的一个变量是 1 (等),或者引用对象。无论哪种方式,该逻辑都是相同的。

the only difference is that the value of x is 1 etc, and ref x is a reference to variable x. In pass by reference, reference-type vs value-type is completely irrelevant; the only important thing is understanding that you pass the value of a variable by default, where the value of a variable is 1 (etc), or "a reference to an object". Either way, the logic is the same.

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

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