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

Support for for null-coalescing assignment (??=, C# 8) #2552

Open
greenozon opened this issue Nov 15, 2021 · 1 comment
Open

Support for for null-coalescing assignment (??=, C# 8) #2552

greenozon opened this issue Nov 15, 2021 · 1 comment
Labels
Enhancement Areas for improvement

Comments

@greenozon
Copy link

Are there any plans to add support for for null-coalescing assignment(??=, C# 8).

Right now ILSpy produces correct code, but it's huge and not very readable

eg:

orig:

        public void test2()
        {
            List<int> nums = null;
            int? i = null;
            // need C# 8.0+
            nums ??= new List<int>();
            nums.Add(i ??= 12);
            nums.Add(i ??= 122);
            Console.WriteLine(string.Join(" ", nums));
            Console.WriteLine(i);
            
        }

Decompiled as:

public void test2()
{
	List<int> nums = null;
	int? i = null;
	if (nums == null)
	{
		nums = new List<int>();
	}
	List<int> list = nums;
	int valueOrDefault = i.GetValueOrDefault();
	int item;
	if (!i.HasValue)
	{
		valueOrDefault = 12;
		i = valueOrDefault;
		item = valueOrDefault;
	}
	else
	{
		item = valueOrDefault;
	}
	list.Add(item);
	List<int> list2 = nums;
	valueOrDefault = i.GetValueOrDefault();
	int item2;
	if (!i.HasValue)
	{
		valueOrDefault = 122;
		i = valueOrDefault;
		item2 = valueOrDefault;
	}
	else
	{
		item2 = valueOrDefault;
	}
	list2.Add(item2);
	Console.WriteLine(string.Join(" ", nums));
	Console.WriteLine(i);
}

@greenozon greenozon added the Enhancement Areas for improvement label Nov 15, 2021
@dgrunwald
Copy link
Member

Currently there are no plans.

The case with reference types is quite readable and I don't see much reason to implement ??= for those.
But the case with value types looks more messy than I would have expected, so that's a good reason to implement support for this feature.

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

No branches or pull requests

2 participants