-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Invalid "Use of unassigned variable". #47585
Comments
Is "TryGetValue" a method that you defined? I cannot find it in the API reference: https://docs.microsoft.com/en-us/dotnet/api/system.collections.specialized.namevaluecollection?view=net-5.0 Also HttpRequest doesn't seem to be available (https://docs.microsoft.com/en-us/dotnet/api/system.web.httprequest?view=net5.0). Here is another repro to simplify tracking this for the compiler team: #nullable enable
using System;
public class MyClass
{
public bool TryGetSomething(out string something)
{
something = "Something";
return true;
}
}
public class C
{
public static void MyMethod(MyClass? myHeader)
{
if (myHeader?.TryGetSomething(out var something) != true)
{
return;
}
Console.WriteLine(something);
}
} |
The project is a netcoreapp3.1. You have to include in your csproj the following line: Anyways, the problem is related to the code analysis. If I change the code to if (request?.Headers == null)
{
return null;
}
if (!request.Headers.TryGetValue(value, out var headers))
{
return null;
}
return headers.Count > 0 ? headers[0] : null; The method compiles without issues. |
Duplicate of #45582. Many people have requested this change, but a definite assignment spec change is required first. |
@RikkiGibson I'm not sure if it's the same issue. The issue described in this report is a change in .NET 5.0, when compiled to .NET Core 3.1 it compiles without errors. That would mean that before .NET 5.0 it wasn't working as per spec, but in .NET 5.0 it was fixed, which is causing this? |
Yeah. What's happening is most likely similar to #52834 (comment) We expect to fix this scenario in C# 10. dotnet/csharplang#4465 |
Version Used:
Using .net5/preview 8
Steps to Reproduce:
The following code was compiling just fine with the 3.1 sdk:
Expected Behavior:
Code compiles
Actual Behavior:
Error compiling: Use of unassigned local variable 'headers' in the return line.
The text was updated successfully, but these errors were encountered: