+2  A: 

You don't need to have a reference to an integer - just put your integer inside a reference type - which is almost what you've done already. Just change this line:

Console.WriteLine(foo);

to:

Console.WriteLine(bar.Value);

Then add an appropriate accessor to the class Bar, and remove the compile errors (remove the ref keywords).

An alternative approach is to pass the integer to a function by reference:

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

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

Output:

8
Mark Byers
Neither of these is exactly what OP is asking for, which seems to be (if I understand correctly) some way to *store* the ref to the local `int foo` in `Main` inside the `Bar` object, such that _other_ method calls on the `Bar` instance change the value of `foo` in `Main`. Not sure that's actually possible in C#, without going into unsafe code...
tzaman
@tzaman, the first one is what the OP is asking for, behaviorwise; remember we've replaced `Main`'s use of `foo` with `bar.Value`. It's really just manual boxing, though, which the OP suggests he doesn't want.
Isaac Cambron
A: 

You didn't specify you were against unsafe code, so this should work:

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

I've never used pointers in C#, but it appears to work. I'm just not sure what the possible side effects are.

David Brown
You are *explicitly forbidden* from doing this. You must *never* store a reference to a stack pointer like this. If the pointer survives after the stack frame is gone, bad things happen. That's why this pattern is not legal in safe code in the first place! **Unsafe code requires you to know what ALL the possible side effects are; that's why it's unsafe.**
Eric Lippert
Well I learn something new every day. Thanks for the explanation. :)
David Brown
+4  A: 

This is a duplicate of

http://stackoverflow.com/questions/2980463/how-do-i-assign-by-reference-to-a-class-field-in-c/2982037#2982037

See my answer there for an explanation of why you cannot store a ref to a variable in a field, and ways of getting around that restriction.

Eric Lippert
+1  A: 

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);
            }
    }
    }
}
tzaman
Thank you! This is what I was looking for. And your BitAccessor looks identical to the one I'd written.The only thing better that'd be nice, would be to be able to apply this to any Integer - but that can't be done without the reference. Your solution works fine for the scope of my project, however.
Jonathon Reinhart
A: 

This doesn't directly answer your question, but can you not just use the System.Collections.BitArray class?

Just wondering if you are "re-inventing the wheel"?

Chris Dunaway