代码之家  ›  专栏  ›  技术社区  ›  Jonathon Reinhart

如何在C#中存储对整数的引用[[副本]

  •  11
  • Jonathon Reinhart  · 技术社区  · 15 年前

    可能重复:
    How do I assign by “reference” to a class field in 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
    

    为了使位存取器能够修改m值,它需要一个对它的引用。但是我想在很多地方使用这个位访问器,只引用所需的整数。

    4 回复  |  直到 8 年前
        1
  •  7
  •   tzaman    15 年前

    不能直接存储对这样的整数的引用,但是 存储对 GlorifiedInt BitAccessor 类嵌套在 荣耀力量 (这样它就可以访问私有字段),然后将引用传递给 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);
                }
        }
        }
    }
    
        2
  •  4
  •   Mark Byers    15 年前

    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
    
        3
  •  -1
  •   David Brown Muad'Dib    15 年前

    您没有指定您是针对不安全代码的,因此这应该可以工作:

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

        4
  •  -1
  •   Chris Dunaway    15 年前

    这并不能直接回答你的问题,但你能不能不用 System.Collections.BitArray

    推荐文章