Issues with `ref` local variables
See original GitHub issueThe following snippet does not compile in the https://try.dot.net browser:
ref int Find(int[,] matrix, Func<int, bool> predicate)
{
for (int i = 0; i < matrix.GetLength(0); i++)
for (int j = 0; j < matrix.GetLength(1); j++)
if (predicate(matrix[i, j]))
return ref matrix[i, j];
throw new InvalidOperationException("Not found");
}
int[,] sourceMatrix = new int[10, 10];
for (int x = 0; x < 10; x++)
for (int y = 0; y < 10; y++)
sourceMatrix[x, y] = x * 10 + y;
ref int item = ref Find(sourceMatrix, (val) => val == 42);
Console.WriteLine(item);
item = 24;
Console.WriteLine(sourceMatrix[4, 2]);
However, copying the ref
return to a value does compile:
ref int Find(int[,] matrix, Func<int, bool> predicate)
{
for (int i = 0; i < matrix.GetLength(0); i++)
for (int j = 0; j < matrix.GetLength(1); j++)
if (predicate(matrix[i, j]))
return ref matrix[i, j];
throw new InvalidOperationException("Not found");
}
int[,] sourceMatrix = new int[10, 10];
for (int x = 0; x < 10; x++)
for (int y = 0; y < 10; y++)
sourceMatrix[x, y] = x * 10 + y;
int item = Find(sourceMatrix, (val) => val == 42);
Console.WriteLine(item);
item = 24;
Console.WriteLine(sourceMatrix[4, 2]);
Note the difference in the 4th line from the end of the sample.
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
c++ - Problems with returning references to local variables...?
You are never allowed to return a reference to a function local variable. All function loval variables are destroyed once the function returns ......
Read more >Local variables cannot be returned by reference.
– Disallow returning local variables by reference. This is the solution that was chosen for C#. - To guarantee that a reference does...
Read more >Declaration statements - var, ref local variables, and ref fields
In this article ... A declaration statement declares a new variable, and optionally, initializes it. All variables have declared type. You can ...
Read more >Referencing ${var.} variable in the 'locals' section fails: you ...
Referencing ${var.} variable in the 'locals' section fails: you can only reference other local variables here #2235.
Read more >Why can't you return a local variable by reference? - Quora
After a function returns, its stack frame is obliterated and local variables no longer exist. Therefore, a reference to a local variable (which...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
In terms of which language features are supported, I’d suggest prototyping against https://try.dot.net?workspaceType=blazor-console.
For scaffolding, for now assume you’re executing inside
Main
. You’ll be able to customize the scaffolding soon, which we should meet to discuss.Ahh. What’s the best way for me to work with you and your team to get a specific behavior on new content?
This is new content, so I have no preferred solution. I want to minimize extra work for all in creating new experiences.