How to store a reference to an integer in C#?

Posted by Jonathon Reinhart on Stack Overflow See other posts from Stack Overflow or by Jonathon Reinhart
Published on 2010-06-06T19:44:39Z Indexed on 2010/06/06 19:52 UTC
Read the original article Hit count: 414

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about reference