Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find all references to fields defined by a C# 9 record?

Repro:

public class TestCase
{
    public Boolean MyFieldIsFour(MyRecord myRecord)
    {
        return myRecord.MyField == 4;
    }
}

public record MyRecord(int MyField);

Right click int MyField and choose Find All References. Visual Studio can't find any references to MyField. I would have expected it to find the reference at myRecord.MyField. If MyRecord is changed to a class then Visual Studio has no problems finding the reference as expected.

Is this a bug in Visual Studio? How can I actually find references now with records?

like image 943
David S. Avatar asked Jan 05 '21 18:01

David S.


People also ask

How do I see all references in Visual Studio?

The Find All References command is available on the context (right-click) menu of the element you want to find references to. Or, if you are a keyboard user, press Shift + F12.

How do you show references in Solution Explorer?

If you see a References node in Solution Explorer, you can use the right-click context menu to choose Add Reference. Or, right-click the project node and select Add > Reference. For more information, see How to: Add or remove references.


1 Answers

This is a bug (or not implemented feature if you want call it that way) in Roslyn.

The semantics around records is a mess. See this feedback, this issue, this issue, this issue, this issue

like image 197
KUTlime Avatar answered Sep 29 '22 10:09

KUTlime