可空数组表示法
c#
我是可空的新手。
可能的符号之间是否存在有意义的差异?
string?[] str
string[]? str
甚至string?[]? str似乎都是有效的
回答
当我们把引用类型放在?后面时,我们允许实例为. 所以我们有null
string?[]- 我们允许项目所在的字符串数组null(但数组本身不能null)string[]?- 可以是null自身的字符串数组(但不能有null项目)string?[]?- 数组及其项目都允许null
- Pre C#8 it is always `string?[]?`
- Great answer. Some confusion might remain though, I hope this doesn't add to it... Both `Array` and `string` are reference types (which can be null), so `string?` and `string` are equivalent, and `[]` and `[]?` are equivalent (and there is no difference between `string?[]?` and `string[]`) in "vanilla" C# regardless of version. For the distinction to be made, nullable reference types as a feature has to be explicitly activated `#nullable enable` because it is an otherwise breaking change to the language which makes such conventionally equivalent statements non-equivalent.