question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issues with `ref` local variables

See original GitHub issue

The 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:closed
  • Created 4 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jonsequiturcommented, Mar 26, 2019

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.

0reactions
BillWagnercommented, Mar 26, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found