Invalid "Use of unassigned variable".
See original GitHub issueVersion Used: Using .net5/preview 8
Steps to Reproduce: The following code was compiling just fine with the 3.1 sdk:
private static string? FindRequestValue(HttpRequest? request, string value)
{
if (request?.Headers?.TryGetValue(value, out var headers) != true)
{
return null;
}
return headers.Count > 0 ? headers[0] : null;
}
Expected Behavior: Code compiles
Actual Behavior: Error compiling: Use of unassigned local variable ‘headers’ in the return line.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Wrong error CS0165: Use of unassigned local variable
Here there is something wrong of the detection that a variable is unassigned: the 'is' keyword always create and assign the variable 'e'....
Read more >Use of unassigned variable 'result'. How do I fix this?
Solution 1. You need to set the result variable to a known value (see below) because when the method exits it tries to...
Read more >Compiler Error CS0165
This error is generated when the compiler encounters a construct that might result in the use of an unassigned variable, even if your...
Read more >Use of unassigned local variable in C#
Here is a simple tip that you may not have run across yet. TLDR: Initialize a variable to null to indicate to the...
Read more >error CS0165: Use of unassigned local variable 'x'
error CS0165 : Use of unassigned local variable 'x'. using System; class A { static void Main() { Func<int, int> x; var a...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
The project is a netcoreapp3.1. You have to include in your csproj the following line:
<FrameworkReference Include="Microsoft.AspNetCore.App" />
to have access to the HttpRequest.Anyways, the problem is related to the code analysis.
If I change the code to
The method compiles without issues.
Yeah. What’s happening is most likely similar to https://github.com/dotnet/roslyn/issues/52834#issuecomment-825749694
We expect to fix this scenario in C# 10. dotnet/csharplang#4465