0

The title pretty much sums it up. I found this code that uses this syntax (in the second to last line) and I'm not sure what it actually does.

var hostname =
    NetworkInformation.GetHostNames()
        .FirstOrDefault(
            hn =>
                hn.Type == hostNameType &&
                hn.IPInformation?.NetworkAdapter != null &&
                hn.IPInformation.NetworkAdapter.NetworkAdapterId == icp.NetworkAdapter.NetworkAdapterId);
lenny
  • 734
  • 2
  • 15
  • 43
  • 3
    It's a [null-conditional operator](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/member-access-operators#null-conditional-operators--and-) – René Vogt Sep 11 '19 at 09:31
  • 1
    `hn.IPInformation?.NetworkAdapter` means "if `hn.IPInformation` is `null` return `null`, if not return `hn.IPInformation.NetworkAdapter`" (often called `null` propagation) – Dmitry Bychenko Sep 11 '19 at 09:33
  • @TomW it's a duplicate. I didn't find that question while searching – lenny Sep 11 '19 at 09:35
  • @lennyy it's the third result for the search query "C# question mark" – Tom W Sep 11 '19 at 09:40
  • @TomW I tried to search for this use of the questionmark specifically as I am aware of the use for nullable fields or parameters – lenny Sep 11 '19 at 09:44
  • @lennyy in English it is formatted as two words, "question mark", this may be the cause of your trouble – Tom W Sep 11 '19 at 09:45
  • @TomW might very well be. thanks for pointing out the duplicate, I was pretty sure someone on here had the same question before but I just couldn't find it. good day to you sir :) – lenny Sep 11 '19 at 09:47

0 Answers0