Reshef’s tip of the day

.net development tips

Tip of the day #2: ref/out key words for reference types

This tip is the courtesy of Gal.

It surprised and embarrassed me to realize that I didn’t know it.

When a reference type is passed to a method without using the ref or out keywords, a new reference is created inside the method and it points to the same location in the memory as the reference that was passed so we have two "pointers" to the same location in memory.

In case the the reference inside the method is set to a different location, the reference that was passed does not change.

When the ref/out keywords are used, we have only one reference inside and outside the method so if the reference is changed to point to another location and it happens inside the method, the outer reference is changed too.

Basic stuff…

Leave a Reply

Jajah is the VoIP player that brought you web-activated telephony.