C# Tip Article
C# error - Cannot initialize a by-reference variable with a value
C# 7.0 introduced "ref return" which allows a method to return "ref" of a value.
Some online articles have wrong examples, which made me to run into
"Cannot initialize a by-reference variable with a value" error.
For example, here is a method using ref return and the method call statement.
// ref return method public ref int GetScore(int id) { //... return ref scores[id]; } // wrong method call ref int s = g.GetScore(1);
This caused "Cannot initialize a by-reference variable with a value" error.
The correct syntax is to add ref in front of GetScore() method call. That is,
// correct method call ref int s = ref g.GetScore(1);