why ref is used, can i omit it? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

why ref is used, can i omit it?

static void Swap<T>(ref T a, ref T b) { T temp = a; a = b; b = temp; }

1st Mar 2017, 10:06 AM
Avid_Coder
Avid_Coder - avatar
2 Answers
+ 3
You can't omit the ref keyword, because the swap function is call by reference.(manipulate in address of original value) If omit the ref key word, the method is call by value, value only swap in the function. You can go back the <method> course and review the concept.
2nd Mar 2017, 12:46 AM
HengJay
HengJay - avatar
+ 2
if you omit it, when you will call your function, no change will happen
1st Mar 2017, 10:07 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar