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

Conditional operators and definite assignment #3659

Closed
AndreasHeisel opened this issue Jul 9, 2020 · 3 comments
Closed

Conditional operators and definite assignment #3659

AndreasHeisel opened this issue Jul 9, 2020 · 3 comments

Comments

@AndreasHeisel
Copy link

I first reported this at the roslyn repository , but was told that this is a language thing.

I purpose to refine the definite assignment specification to cover the case described below:

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

@yaakov-h
Copy link
Member

yaakov-h commented Jul 9, 2020

#3365 proposes to make this possible.

@jnm2
Copy link
Contributor

jnm2 commented Jul 9, 2020

Direct duplicate of #3365

@333fred
Copy link
Member

333fred commented Jul 9, 2020

Closing as a duplicate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants