What does ref means

Steven Schveighoffer schveiguy at yahoo.com
Tue Sep 6 04:33:13 PDT 2011


On Tue, 06 Sep 2011 05:28:22 -0400, malio <youdontwanttoknow at unknown.com>  
wrote:

> Hi guys,
>
> I'm a bit confused what exactly ref means and in which cases I  
> definitely need this keyword.

ref is simple.  It's a pointer, but without the messy pointer syntax.   
These two programs are exactly the same (will generate the same code):

void foo(int *i)
{
    *i = 5;
}

void main()
{
    int x = 2;
    foo(&x);
}

----------------------

void foo(ref int i)
{
    i = 5;
}

void main()
{
    int x = 2;
    foo(x); // note, there's no need to use &, the compiler does it for you
}

-Steve


More information about the Digitalmars-d-learn mailing list