Skip to content
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

Null-conditional operator and CS0165 #45776

Closed
AndreasHeisel opened this issue Jul 8, 2020 · 1 comment
Closed

Null-conditional operator and CS0165 #45776

AndreasHeisel opened this issue Jul 8, 2020 · 1 comment
Labels
Area-Compilers Resolution-By Design The behavior reported in the issue matches the current design

Comments

@AndreasHeisel
Copy link

I don't understand why I get an error if I use ?. but not if I use != null in the example below.

Version Used:

Visual Studio 16.6.3, .NET Core 3.1 Console Application

Steps to Reproduce:

using System.Collections.Generic;

namespace RoslynTest
{
	class Program
	{
		class SomeClass
		{
			public Dictionary<string, string> SomeProperty { get; } = new Dictionary<string, string>();
		}

		static SomeClass? MyProperty { get; }

		static void Main(string[] args)
		{
			{
				// Doesn:t work
				if (MyProperty?.SomeProperty.TryGetValue("key", out var value) == true)
				{
					//Error CS0165  Use of unassigned local variable 'value'  RoslynTest C:\Users\h165\source\repos\RoslynTest\RoslynTest\Program.cs 21  Active
					value.ToString();
				}


				// Works
				if (MyProperty != null && MyProperty.SomeProperty.TryGetValue("key", out var value2))
				{
					//no error
					value.ToString();
				}
			}
		}
	}
}

Expected Behavior:

No errors

Actual Behavior:

CS0165

@RikkiGibson RikkiGibson added Resolution-By Design The behavior reported in the issue matches the current design and removed New Language Feature - Nullable Reference Types Nullable Reference Types labels Jul 8, 2020
@RikkiGibson
Copy link
Contributor

This is definite assignment analysis working as specified. Please see #45582 (comment) and consider searching for or filing an issue at https://github.com/dotnet/csharplang if you wish to propose a change to the specification.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Resolution-By Design The behavior reported in the issue matches the current design
Projects
None yet
Development

No branches or pull requests

3 participants