我可以存储在一个对象的变量的引用? [英] Can I store a reference to an variable in an object?

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

问题描述

可能重复:结果
在C#中存储到一个对象的引用

Possible Duplicate:
Storing a reference to an object in C#

有没有存储在一个类对象引用的方法吗?是这样的:

Is there a way to store a reference in a class object? Something like:

public class X
{
  public X(ref int i)
  {
    _t = i;
  }

  public void f(int ii)
  {
    _i = ii;
  }

  private int ref _i;
}

// ...
int i = 0;
X x = new X(ref i);
x.f(42);
// i is now 42

在C ++中我会用指针或引用做到这一点。

In C++ I'd do this with pointers or references.

推荐答案

您可以与非托管代码做到这一点,但没有一个托管代码的方式有一个值类型的引用。你不得不手动将其框变成某种对象容器,并有一个参考。

You can do it with unmanaged code, but there isn't a managed code way to have a reference to a value type. You'd have to manually box it into some kind of object container and have a reference to that.

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

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