-
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
Should warn for ?. in expression of foreach #13023
Comments
OR just tacitly handle foreach |
Are you saying that: static void Main(string[] args)
{
Foo foo = new Foo();
foreach (int j in foo.bar?.list)
{
System.Console.WriteLine(j);
}
} Should translate to: static void Main(string[] args)
{
Foo foo = new Foo();
foreach (int j in foo.bar != null
? foo.bar.list
: Enumerable.Empty<int>().ToList())
{
System.Console.WriteLine(j);
}
}
static void Main(string[] args)
{
Foo foo = new Foo();
foreach (int j in null as List<int>)
{
System.Console.WriteLine(j);
}
}
A simple fix for this is the static void Main(string[] args)
{
Foo foo = new Foo();
foreach (int j in foo.bar?.list ?? Enumerable.Empty<int>())
{
System.Console.WriteLine(j);
}
} |
I think a warning would be appropriate here. By using |
@svick That is actually what I mean |
This would probably automatically get picked up by #5032 as a possible dereference of a nullable reference type. |
What if |
Theres no WAY to handle
?.
operator with foreach.It should emit compile warning or error.
it just thorws NullReferencedException when nullptr object in it(from GetEnumerator)
The text was updated successfully, but these errors were encountered: