Rider Syntax for Primary Constructor Parameters Assigned to Private Readonly Variables Appears Misleading
I thought about putting in an issue, but maybe I'm misinterpreting the intent of the syntax highlighting so I'm sharing here first to see what the response will be.
When I use primary constructor variables and assign them to read only fields, the syntax highlighting is grayed out. When I hover it, it suggests to use a primary constructor variables. Well yeah, I'm using that to assign those variables as readonly fields to keep its use intent intact since primary constructor variables are mutable. The majority of the dependencies of a class (for me) are always immutable and readonly.
For some reason this behavior in the IDE just gets under my skin. I WANT them grayed out when they're not being used (like usual) and I WANT them back to being highlighted when I use them. Is there any reason why primary constructor variables assigned to private readonly fields does not behave in this way? Is it possible it's a corner case issue of syntax highlighting they haven't gotten to yet, or is it the intent of Rider to dictate mutability intent of variables being passed as primary constructor arguments?

Please sign in to leave a comment.
Hello,
Thanks for contacting Rider support. What you observe is expected behavior. It happens when Rider detects that readonly field definition can be replaced by primary constructor parameter. If you don't need this suggestion, you can open Conext Action then set inspection severity to “Do not show”. Thus the greyed-out highlight will disappear.
Let me know if you have any more questions.
Reference
Primary Construtor Conversion
Regards,
Tom
Ok, I turned it off. I'm trying to weigh the pros and cons of really using these primary constructors and I'm beginning to think maybe for much of my work, they're not really needed:
https://andrewlock.net/thoughts-about-primary-constructors-3-pros-and-5-cons/
Since there's intent to keep dependencies readonly, primary constructors seem good for specific use cases more so than just good old bread and butter dependency injection. If it is the case that the IDE is suggesting intent on use based on primary constructor variable usage, then I would like some more documentation from sources that say “using primary constructors changes the intent of your use of dependencies and you should use the old constructor way for dependency injection when your dependencies are readonly”. The documentation you point that says the “old habit of assigning readonly fields” is indeed the recommended way of assigning dependencies as stated in the Microsoft documentation, so it's a best practice, not just a habit.
I'll keep noodling this over. Thanks for your feedback.