如何存储在C#中的一个整数的参考? [英] How to store a reference to an integer in C#?

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

问题描述

可能重复:结果
如何分配由“参考与rdquo;的在C#类字段

大家好 - ?告诉我如何做这项工作?基本上,我需要一个整数引用类型为(int *将会用C工作++)

Hello everyone - tell me how to make this work? Basically, I need an integer reference type (int* would work in 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'
   }
 }



我必须?使用拳击

Do I have to use boxing?

编辑:
也许我本来应该更具体。我正在写一个BitAccessor类,即只允许访问各个位。这是我所希望的用法:

Perhaps I should have been more specific. I'm writing a BitAccessor class, that simply allows access to individual bits. Here's my desired usage:

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



用法:

Usage:

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

有关BitAccessor要能够修改m_val,它需要对它的引用。但是,我想用这个BitAccessor很多地方,只有到所需整数参考。

For BitAccessor to be able to modify m_val, it needs a reference to it. But I want to use this BitAccessor many places, with just a reference to the desired integer.

推荐答案

您不能存储参照这样的整数直接,但你的可以的存储包含它的 GlorifiedInt 对象的引用。在你的情况,我可能会做的就是让嵌套在 GlorifiedInt BitAccessor ​​类(因此它获得访问私有字段),然后将它传递给这个时,它的创建,它可以再用来访问 m_val 字段。下面是一个例子做你要找的是什么:

You can't store a reference to an integer like that directly, but you can store a reference to the GlorifiedInt object containing it. In your case, what I'd probably do is make the BitAccessor class nested inside GlorifiedInt (so that it gets access to private fields), and then pass it a reference to this when it's created, which it can then use to access the m_val field. Here's an example which does what you're looking for:

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);
            }
    }
    }
}

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

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