Do int ref parameter get boxed?

后端 未结 6 1916
-上瘾入骨i
-上瘾入骨i 2021-02-12 13:14

Say I have the following code:

void Main()
{
    int a = 5;
    f1(ref a);
}

public void f1(ref int a)
{
    if(a > 7) return;
    a++;
    f1(ref a);
    Co         


        
6条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-12 13:41

    Adding to the existing answers how this is implemented:

    The CLR supports so called managed pointers. ref passes a managed pointer to the variable on the stack. You can also pass heap locations:

    var array = new int[1];
    F(ref array[0]);
    

    You can also pass references to fields.

    This does not result in pinning. Managed pointers are understood by the runtime (in particular by the GC). They are relocatable. They are safe and verifiable.

提交回复
热议问题