Intellisense suggesting code improvement not supported for the project's C# version
See original GitHub issueThis issue has been moved from a ticket on Developer Community.
[severity:It bothers me. A fix would be nice] I have this code:
public class MyClass { public int ResponseCode { get; set; } public IList<AnotherClass> ResponseArray { get; set; } public string ResponseString { get; set; } } MyClass instance = new MyClass(); VS Intellisense shows:
‘new’ expression can be simplified
and upon selecting that action, it changes new MyClass()
to just new()
, so that the line of code becomes:
MyClass instance = new();
but then build fails with error code CS8181:
‘new’ cannot be used with tuple type. Use a tuple literal expression instead.
I think Intellisense has this bug where it’s recommending a code change that’s not supported for current C# version 6.0.
Microsoft Visual Studio Professional 2019 Version 16.11.7 .Net framework 4.6.1
[stackoverflow question] (https://stackoverflow.com/questions/70210593/visual-studio-intellisense-shows-new-expression-can-be-simplified-but-that-fa)
Original Comments
Feedback Bot on 12/5/2021, 06:44 PM:
We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.
Original Solutions
(no solutions)
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (7 by maintainers)
@Sergio0694 Starting with #36924,
CSharpUseRangeOperatorDiagnosticAnalyzer
andCSharpUseIndexOperatorDiagnosticAnalyzer
are gated on the existence ofSystem.Range
andSystem.Index
, respectively.The original issue above is a duplicate of #52727. It’s impossible for the code fix to be offered in a project targeting C# 6, which means the file was included in more than one project and triggered from a project targeting C# 9 or newer. This is exactly the scenario described by #52727.