question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Invalid "Use of unassigned variable".

See original GitHub issue

Version 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:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
CheloXLcommented, Sep 10, 2020

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

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.

0reactions
RikkiGibsoncommented, May 9, 2021

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 https://github.com/dotnet/roslyn/issues/52834#issuecomment-825749694

We expect to fix this scenario in C# 10. dotnet/csharplang#4465

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found