roslyn: CodeLens reference count is not shown for const fields

Version Used: VS 2017 15.8.3, 15.9.1, 15.9.5

Steps to Reproduce:

  1. Create any C# class/code with const
  2. Use the const in other class or in other method in the same class
  3. Look at the const declaration

Expected Behavior: The const should show how many (count of total) references in the C# project

Actual Behavior: Total references are not shown.

Sample code: For const:

    public class MiscelanousConstants
    {
        public const decimal SPEED_OF_LIGHT = 299792458M;
        public const decimal MATH_PI = 3.14123123M;
        public const decimal ONE_PARSEC = 3.26M;
    }

Sample use of the const:

    class AstronomicDistanceCalculator
    {
        public static decimal CalculateLightYears(decimal yearsFraction)
        {
            return Decimal.Multiply(yearsFraction, Decimal.Multiply(MiscelanousConstants.SPEED_OF_LIGHT, (365 * 24 *3600)));
        }
        public static decimal ConvertParsecToLightYears(decimal parsec)
        {
            return (decimal)(parsec * CalculateLightYears(MiscelanousConstants.ONE_PARSEC));
        }
        public static decimal CalculateLightSeconds(Int32 seconds)
        {
            return Decimal.Multiply((decimal)seconds, MiscelanousConstants.SPEED_OF_LIGHT);
        }
    }
    class Program
    {
        const decimal ABSOLUTE_ZERO = 0.0M;

        static void Main(string[] args)
        {
            decimal oneLightSecond = MiscelanousConstants.SPEED_OF_LIGHT;
            Console.WriteLine("one light second = " + oneLightSecond + "m");
            Console.WriteLine("one parsec = " + AstronomicDistanceCalculator.ConvertParsecToLightYears(1) + "m");
            Console.WriteLine("zero = " + ABSOLUTE_ZERO);
            Console.ReadLine();
        }
    }

On VS 2017, the const declaration has no total of type references:

image

Only the enclosing MiscelanousConstants class that has the types has total references shown.

But when using “Find All References” on MiscelanousConstants.SPEED_OF_LIGHT, I get correct number of the references:

image

The same problem happens on the const of ABSOLUTE_ZERO:

image

Finally, my questions are:

  1. Is this by design?
  2. If is this by design, why const is not showing total references before the declaration, like other type/member such as class, method, field and properties?

If this is not by design and easy to fix, I’d like to propose a PR later.

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 29
  • Comments: 31 (15 by maintainers)

Most upvoted comments

it would be awesome to have this as opted in. I sometimes switched to using properties just to see the references, though i would have liked to use consts instead.

Having code lens for constants would be helpful for quick code navigation, it’s obvious. Maybe there are some technical problems, but “Find All References” finds them, why not code lens.

I Agree

@eriawan no plans as of yet. This is in the backlog currently. The design review just lays out the changes we would take if this gets scheduled for a future sprint. Thanks!

Design meeting review 4/25/2022.

  1. We believe codelens should be enabled on Fields/Constants.
  2. There is unlikely to be any significant perf overhead over and above what you get if you’re already using codelens already.
  3. Because of the potential to impact the UI for an experience that has been relatively unchanged for 10+ years, this likely should be paired with an option to allow users to turn this off if they do not like having these new codelens items show up.

We should drive a telemetry effort to see how many people have codelens on, but then disable this new option. If it turns out that there is practically no userbase that turns this off, we can consider removing the option later.

Having this as a toggle-able option would be a great addition. I’m currently re-organizing some code, and had to change all the constants to Get-Only Properties in order to see where they are used and how many times easily.

@CyrusNajmabadi

This is a great news! Is there any definite plan on when this feature will be enabled? I hope we could see this extended CodeLens on constants and fields in the next preview update of VS 2022, the 17.3.0 Preview!

@eriawan no need to apologize. I just prefer to answer in as straightforward was as possible if someone’s offering help and we aren’t in a position it accept it. 😄

Fixing this issue (and other cases where CodeLens is desired for code elements that it doesn’t consider today) relies on porting and modifying a relatively large amount of internal code to open source. The code is generally unchanged over time, so it’s low maintenance as long as the supported features don’t change. It’s not clear when this work would be scheduled, since it would require taking resources away from other active items.

@ShadowMarker789 right-click on the constants and click find all references, which can help you find where the constants were referenced in your code.