Is c# ref int foo implemented as C++ int **foo?
.net, c#
Solution
In C#, when you have a method
void M(ref int f) { }
and you call it
int x = 123;
M(ref int x):
how does this work?
Logically, this means "x and f refer to the same storage location".
The way we actually implement that in the CLR is f is of type "managed reference to variable that can contain an integer". We pass the managed address of local variable x to M.
The analog of that in C++ would be a method that takes an &int -- a reference to a variable that can contain an int.
Is that clear?
Problem
I was talking with a coworker about this yesterday and it got me thinking about .Net's pass by reference. ``` // C# class Foo {} static void Test(ref Foo foo) { ... }; static void Main() { Foo f; Test(ref foo); } ``` Has to be implemented with a double indirection because we're changing the value of the pointer. Because all reference types are references (pointers) ``` // C# static void Test(Foo foo) { ... } static void Test(ref Foo foo) { ... }; ``` equates to something like ``` // C++ void Test(Foo *foo); void Test(Foo **foo); ``` But if this is a VALUE type, we don't actually need the double indirection. So I'm curious if ``` // C# static void Test(ref int bar) { ... } ``` becomes ``` // C++ void Test(int *bar); // or void Test(int **bar); ``` 1/29/10 Update: Reading all the answers, I realize that I was not exactly clear enough in what I wanted and I was misleading by throwing in C++ to what was going on. What I was primarily interested in was how it was implemented in the CLR and what the JIT would do to produce assembly for it. Thanks for all the answers, I found them all to be correct from a perspective but I chose the one that was closest to the question I thought I had asked.