C#:引用/指针数组了一些整数 [英] C#: Array of references / pointers to a number of integers

查看:125
本文介绍了C#:引用/指针数组了一些整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想持有一个数组一些短裤引用。我以为我可以创建短裤,然后将它们添加到数组。所以......每一个被引用的对象改变时,这一点也反映在阵列中,反之亦然。做一些试验使我确信,它并不完全这样的。事实上,它看起来像值传送而不是引用。

I would like to hold references to a number of shorts in an array. I assumed I could just create the shorts and then add them to the array. So... every time the referenced object is changed, this is reflected in the array, and vice versa. Doing some trials convinced me that it does not quite work that way. In fact, it looks like the value is transferred but not a reference.

下面的代码创建两个短裤,将这些以数组作为对象,然后改变原来的短。然而,当访问假定引用短数组它并没有改变,这使我相信,这是一个无关与原来的一个全新的对象英寸

Below code creates two shorts, adds these to an array as objects, then changes the original short. However, when accessing the assumed referenced short in the array it has not changed, which leads me to believe that it is a wholly new object that has nothing to do with the original one.

        Console.WriteLine("Testing simple references:");
        short v1 = 1;
        short v2 = 2;
        object[] vs = new object[2];
        vs[0] = v1;
        vs[1] = v2;
        v1 = 1024;
        v2 = 512;
        Console.WriteLine(" v1: " + (short)vs[0]);
        Console.WriteLine(" v2: " + (short)vs[1]);



我误解的东西在这里根本,将不胜感激,如果有人可以解释,也许指向我一个解决方案这将做我想做的。

I am misunderstanding something fundamental here and would appreciate if someone could explain, and perhaps point me to a solution that would do what I want.

推荐答案

有在C#的类型系统值类型和引用类型。

There are two kinds of types in the C# type system "value types" and "reference types".

值类型是按值复制;当你复制一个,你得到无关与原来的一个全新的对象。

Value types are copied by value; when you copy one, you get a wholly new object that has nothing to do with the original.

引用类型被引用复制;当你复制一个,你实际上是复制到一些存储位置的引用。你得到两个引用这两个引用一个对象。

Reference types are copied by reference; when you copy one, you are actually copying a reference to some storage location. You get two references that both refer to one object.

短裤是值类型。

如果你想有一个总之是引用类型,那么你可以做一个参考类型包装:

If you want a short to be a reference type, then you could make a reference type wrapper:

class ReferenceType<T> where T : struct
{
    public T Value { get; set }
    public ReferenceType(T value) { this.Value = value; }
}

var v1 = new ReferenceType<short>(1);
var v2 = new ReferenceType<short>(2);
var vs = new ReferenceType<short>[2] { v1, v2 };
v1.Value = 1024;
v2.Value = 512;
Console.WriteLine(vs[0].Value);
Console.WriteLine(vs[1].Value);

和你去那里。

现在,这将使你引用访问的短期的,因为短期实际存储在与类的价值属性相关联的领域。如果你然后说:

Now, that will give you reference access to the short because the short is actually stored in the field associated with the value property of the class. If you then say:

v2 = new ReferenceType<short>(3);
Console.WriteLine(vs[1].Value);

您不会得到3 - 现在v2上指的是不同的对象比VS [1 ]。如果你的真正的想捕捉是的变量的,那么你要使用的是一个的闭合

you won't get "3" -- v2 now refers to a different object than vs[1]. If what you really want to capture is a reference to a variable then what you want to use is a closure.

class ReferenceToVariable<T>
{
    private Func<T> getter;
    private Action<T> setter;
    public ReferenceToVariable(Func<T> getter, Action<T> setter) 
    { 
        this.getter = getter;
        this.setter = setter;
    }
    public T Value { get { return getter(); } set { setter(value); } }
}
...
short v1 = 1;
short v2 = 2;
var vs = new [] 
{ 
    new ReferenceToVariable<short>(()=>v1, x=>{v1=x;}),
    new ReferenceToVariable<short>(()=>v2, x=>{v2=x;})
};
v1 = 123;
vs[1].Value = 456;
Console.WriteLine(vs[0].Value); // 123
Console.WriteLine(v2); // 456

下面,我们在知道如何获取和设置的当前值的数组对象捕捉V1和V2。

Here we capture in the array objects which know how to get and set the current values of v1 and v2.

现在,如果你想要做的是使一个的别名的另一个变量的直接的,没有这个对象的方式,那么只有一做就是在C#的方式:

Now, if what you want to do is make an alias to another variable directly, without this object in the way, then there is only one way to do that in C#:

void M(ref short x)
{
    x = 123;
}
...
short y = 1;
M(ref y);

现在的x和y两个名字相同的变量。然而,概念做一个别名另一个变量只适用于C#的时候走样变量是方法的形式参数。有没有办法做到这一点一般。

Now "x" and "y" are two names for the same variable. However, the concept of "make an alias to another variable" only works in C# when the aliasing variable is a formal parameter of a method. There is no way to do it in general.

现在,我们在理论上可以做这样的事情,你想要什么。我们可以支持REF当地人:

Now, we could in theory do something like what you want. We could support "ref locals":

short v1 = 1;
ref short rv1 = ref v1;
rv1 = 123;
Console.WriteLine(v1); // 123



也就是说,RV1成为V1的别名。 C#不支持这一点,但确实CLR,因此,我们可以支持它。然而,CLR确实的的支持使得裁判的元素类型,或存储裁判领域的数组。因此,在这个意义上说,你不能做你想做的。

That is, rv1 becomes an alias for v1. C# does not support this, but the CLR does and therefore we could support it. However, the CLR does not support making arrays of "ref" element type, or fields that store refs. So in that sense, you couldn't do what you want.

C#不支持一些特殊的隐藏功能传递围绕像引用变量,但对象更轻的重量比上述两个代表基准。然而,这些特殊功能只适用于离奇互操作场景,我建议反对他们。 (再次,你不能让一个数组存储类型引用)。我不认为我会谈论这些功能更多的这个答案;你真的不想去那里,相信我。

C# does support some special "hidden" features for passing around objects that act like references to variables but are lighter weight than the "two delegate" reference mentioned above. However, these special features are only for bizarre interop scenarios and I recommend against them. (And again, you can't make an array that stores typed references.) I don't think I'll talk about those features more in this answer; you really don't want to go there, believe me.

这篇关于C#:引用/指针数组了一些整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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