-2

Sorry for asking a simple question, I'm new to Azure Function HTTPtriggers with C#, Anyone know what is the name = name?? data?.name; mean in c# ?

        string name = req.Query["name"];
        string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
        dynamic data = JsonConvert.DeserializeObject(requestBody);
        name = name ?? data?.name;
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
giga
  • 1
  • 1
  • 2
  • If `name` was not `null` take that value, if it was, try taking `data?.name`. The expression `data?.name` looks if `data` was `null`, if so then `null` is returned, otherwise the value of `data.name` is returned. – Alexey S. Larionov Jan 21 '21 at 19:07
  • You have 2 questions, both are answered with an extremely simple Google search: "c# double question mark" and "c# question mark with dot". Or just the C# documentation – Camilo Terevinto Jan 21 '21 at 19:11
  • Thank you for the comment @AlexeyLarionov – giga Jan 21 '21 at 19:16
  • Just want to make sure how both of them put together mean. thank you for all the explanation, and relate question link, @CamiloTerevinto – giga Jan 21 '21 at 19:19

1 Answers1

-1

It essentially means

if name is not null
  set name to name
else
  if data is null
    set name to null
  else
    set name to data.name

The second operator (?.) avoids a NullReferenceException by returning null instead of trying to use the access modifier. The first (??) returns the first operand if the value is not null, otherwise it returns the second.

Note that neither of these are specific to dynamic or Azure.

It could also have been written as

if ((name == null) && (data != null))
{
   name = data.name;
}
D Stanley
  • 149,601
  • 11
  • 178
  • 240