Can there be special treatment for foreach ( var item in xx ?? [ ])
#9614
Answered
by
huoyaoyuan
zms9110750
asked this question in
Q&A
-
public class TreeNode<T>(T value) : IList<TreeNode<T>>
{
// ...
}now: foreach (var node in TreeNode ?? [] ) //CS0019foreach (var node in TreeNode ?? (TreeNode<int>) [] ) // CS9214foreach (var node in TreeNode as IEnumerable<TreeNode<int>> ?? []) // Work
``
|
Beta Was this translation helpful? Give feedback.
Answered by
huoyaoyuan
Aug 13, 2025
Replies: 3 comments 4 replies
-
|
It's effectively a combination of #2473 and #9562, and the former was rejected because of breaking change. With linq, |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
zms9110750
This comment was marked as off-topic.
This comment was marked as off-topic.
-
|
This may be a use case for |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's effectively a combination of #2473 and #9562, and the former was rejected because of breaking change.
With linq,
.AsEnumerable() ?? []is a less-verbose solution.