1

I would like to add MapWinGis to my C# application ,but I don't know the following:

Whats the difference between (examples 1 and 2) and the meaning of the [question mark] in the first sample?


Shapefile.Categories.Item[n]?.MinValue

VS

Shapefile.Categories.Item[n].MinValue
Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
Steve
  • 13
  • 2
  • What is "`MapWinGis`" ? – Dai Jul 16 '23 at 11:10
  • The expression `Shapefile.Categories.Item[n]?.MinValue` will evaluate to `Nothing` (aka `null` in C#) if `..Item[n]` is also `Nothing` - but it will throw if `Items[n]` does not exist - while `Shapefile.Categories.Item[n].MinValue` will throw an NRE if `Shapefile.Categories.Item[n]` is `Nothing`. – Dai Jul 16 '23 at 11:10
  • 1
    @Dai MapWinGis: [link](https://www.mapwindow.org/mapwingis/getting_started.html), This means: **Shapefile.Categories.Item[n]?.MinValue** EQUALS **Shapefile.Categories.Item[n]== null ? null : Shapefile.Categories.Item[n].MinValue** ? – Steve Jul 16 '23 at 11:19

1 Answers1

1

The ? symbol is the null-conditional operator. A null-conditional operator applies member access, ?., or element access, ?[], operation to its operand only if that operand evaluates to non-null; otherwise, it returns null.

  • If a evaluates to null, the result of a?.x or a?[x] is null.
  • If a evaluates to non-null, the result of a?.x or a?[x] is the same as the result of a.x or a[x], respectively.

Reference: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/member-access-operators#null-conditional-operators--and-

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
  • 1
    i prefer to call this null-propagation operator, cause of it actually propagates null value for the whole expression. There is such rule in [code style](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0031). In docs probably it also was named as null propagation operator, but changed some time ago. – Ryan Jul 16 '23 at 11:28
  • @VivekNuna and Ryan thanks very much – Steve Jul 16 '23 at 12:18