11

可能重复:
如何通过“引用”分配给 c# 中的类字段?

大家好 - 告诉我如何进行这项工作?基本上,我需要一个整数引用类型(int* 可以在 C++ 中工作)

class Bar
{
   private ref int m_ref;     // This doesn't exist

   public A(ref int val)
   {
      m_ref = val;
   }

   public void AddOne()
   {
      m_ref++;
   }
}

class Program
{
   static void main()
   {
      int foo = 7;
      Bar b = new Bar(ref foo);
      b.AddOne();
      Console.WriteLine(foo);    // This should print '8'
   }
 }

我必须使用拳击吗?

编辑: 也许我应该更具体。我正在编写一个 BitAccessor 类,它只允许访问单个位。这是我想要的用法:

class MyGlorifiedInt
{
   private int m_val;
   ...
   public BitAccessor Bits {
      return new BitAccessor(m_val);
   }
}

用法:

MyGlorifiedInt val = new MyGlorifiedInt(7);
val.Bits[0] = false;     // Bits gets a new BitAccessor
Console.WriteLine(val);  // outputs 6

要使 BitAccessor 能够修改 m_val,它需要对其进行引用。但我想在很多地方使用这个 BitAccessor,只需要引用所需的整数。

4

4 回答 4

7

您不能直接存储对整数的引用,但可以存储对GlorifiedInt包含它的对象的引用。在您的情况下,我可能会做的是将BitAccessor类嵌套在内部GlorifiedInt(以便它可以访问私有字段),然后将它的引用传递给this它的创建时间,然后它可以用来访问该m_val字段。这是一个示例,它可以满足您的需求:

class Program
{
    static void Main(string[] args)
    {
        var g = new GlorifiedInt(7);
        g.Bits[0] = false;
        Console.WriteLine(g.Value); // prints "6"
    }
}

class GlorifiedInt
{
    private int m_val;

    public GlorifiedInt(int value)
    {
        m_val = value;
    }

    public int Value
    {
        get { return m_val; }
    }

    public BitAccessor Bits
    {
        get { return new BitAccessor(this); }
    }

    public class BitAccessor
    {
        private GlorifiedInt gi;

        public BitAccessor(GlorifiedInt glorified)
        {
            gi = glorified;
        }

        public bool this[int index]
        {
            get 
            {
                if (index < 0 || index > 31)
                    throw new IndexOutOfRangeException("BitAcessor");
                return (1 & (gi.m_val >> index)) == 1; 
            }
            set 
            {
                if (index < 0 || index > 31)
                    throw new IndexOutOfRangeException("BitAcessor");
                if (value)
                    gi.m_val |= 1 << index;
                else
                    gi.m_val &= ~(1 << index);
            }
    }
    }
}
于 2010-06-07T05:36:08.593 回答
4

您不需要引用整数 - 只需将整数放入引用类型中 - 这几乎是您已经完成的。只需更改此行:

Console.WriteLine(foo);

到:

Console.WriteLine(bar.Value);

然后为类添加适当的访问器Bar,并删除编译错误(删除ref关键字)。

另一种方法是通过引用将整数传递给函数:

static void AddOne(ref int i)
{
    i++;
}

static void Main()
{
    int foo = 7;
    AddOne(ref foo);
    Console.WriteLine(foo);
}

输出:

8
于 2010-06-06T19:46:22.870 回答
-1

您没有指定您反对不安全的代码,所以这应该有效:

unsafe class Bar {
    private int* m_ref;

    public Bar(int* val) {
        m_ref = val;
    }

    public void AddOne() {
        *m_ref += 1;
    }
}

unsafe class Program {
    static void Main() {
        int foo = 7;
        Bar b = new Bar(&foo);
        b.AddOne();
        Console.WriteLine(foo);    // prints 8
        Console.ReadLine();
    }
}

我从未在 C# 中使用过指针,但它似乎可以工作。我只是不确定可能的副作用是什么。

于 2010-06-06T20:16:14.917 回答
-1

这并不能直接回答您的问题,但您不能只使用System.Collections.BitArray类吗?

只是想知道您是否在“重新发明轮子”?

于 2010-06-07T18:11:30.007 回答