存储在C#中引用一个对象 [英] Storing a reference to an object in C#

查看:141
本文介绍了存储在C#中引用一个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何可以存储一个参考.NET对象。

I was wondering how one could store a reference to an object in .net.

也就是说,我想类似下面的code(注意,当然,下面的code可以路要走,从如何真正做到这一点):

That is, I would like something like the following code (note, of course, that the following code may be way off from how to actually do it):

class Test
{
    private /*reference to*/ Object a;
    public Test(ref int a)
    {
        this.a = a;
        this.a = ((int)this.a) + 1;
    }
    public Object getA() { return this.a; }
}
/*
 * ...
 */
static void Main(string[] args)
{
    int a;
    a=3;
    Test t = new Test(ref a);
    Console.WriteLine(a);
    Console.WriteLine(t.getA());
    Console.ReadKey();
}

要产生下面的输出:

4
4

在理想情况下,我想做到这一点,而无需编写一个包装类各地的整数。

Ideally, I would like to do this without writing a wrapper class around the integer.

换句话说,我想我要在净指针。

In other words, I think I want pointers in .Net.

推荐答案

您不能存储引用的变量的.NET中,期。您可以存储引用的对象的,而不是引用的变量的。

You cannot store references to variables in .NET, period. You can store references to objects, but not references to variables.

原因是,如果你被允许存储引用任何变量,那么你可以存储引用局部变量。如果你可以存储引用局部变量则运行时不能使用在短期内存池存储本地变量的优化,又名,堆栈。

The reason is that if you were allowed to store references to arbitrary variables then you could store references to local variables. If you can store references to local variables then the runtime cannot use the optimization of storing local variables on the short-lived memory pool, aka, the stack.

现在,即使你能做到这一点,你所描述的操作不类型安全出于不同的原因。你有一个(非常严重命名)字段变量A型参考对象变量和(非常糟糕和容易混淆的名字命名)局部变量的A型参考int变量。即使你可以存储的引用变量它没有任何意义存储的引用,int变量的东西的类型参考对象变量,因为这两种类型在逻辑上是不兼容的。你可以对它们执行的操作是不同的;引用一个对象变量可以写入到它的字符串;引用一个int变量不能。

Now, even if you could do that, the operation you are describing is not typesafe for a different reason. You have a (very badly named) field variable "a" of type "reference to object variable" and a (very badly and confusingly named) local variable "a" of type "reference to int variable". Even if you could store a reference to a variable it doesn't make any sense to store a reference to an int variable in something of type "reference to object variable" because those two types are logically not compatible. The operations you can perform on them are different; a reference to an object variable can have a string written into it; a reference to an int variable cannot.

也许我误解,但不会一个变量,如整数上方盒装入然后可以存储为一个引用一个对象?

Perhaps I am misunderstanding but wouldn't a variable such as the integer above be boxed into an object which could then be stored as a reference?

您是混乱引用的对象的与引用的变量的。这是混淆,我们使用相同的术语什么是真正的两码事。

You are confusing references to objects with references to variables. It is confusing that we use the same terminology for what is really two different things.

是的,拳把一个值类型,如int,转换为引用类型,比如对象。这已经完全没有任何关系一起的的变量引用的。

Yes, boxing turns a value type, like int, into a reference type, like object. That has ABSOLUTELY NOTHING WHATSOEVER to do with references to variables.

当你犯了一个裁判,你正在一个别名,该变量的变量。当你说

When you make a ref to a variable you are making an alias for that variable. When you say

void M(ref int y) { y = 123; }
...
int x = 0;
M(ref x);

你说x和y是两个不同的名称相同的变量。

you are saying "x and y are two different names for the same variable".

现在,如果你想要做的是重新present的概念,我已经抓获了变数,我希望能够读取和写入,然后使用委托:

Now, if what you want to do is represent the notion of "I have captured a variable and I want to be able to read and write it" then use delegates:

class Ref<T>
{
    private Func<T> getter;
    private Action<T> setter;
    public Ref(Func<T> getter, Action<T> setter)
    {
        this.getter = getter;
        this.setter = setter;
    }
    public T Value
    {
        get { return getter(); }
        set { setter(value); }
    }
}
...
int abc = 123;
var refabc = new Ref<int>(()=>abc, x=>{abc=x;});
... now you can pass around refabc, store it in a field, and so on
refabc.Value = 456;
Console.WriteLine(abc); // 456
Console.WriteLine(refabc.Value); // 456

有意义吗?

这篇关于存储在C#中引用一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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