?? Two line operator?

In this .NET code:

return Redirect("~/Home" ?? "JaneDoe");

If I read the documents correctly, the ??" operator works similarly to IsNull in SQL:

IsNull("~/Home", "JaneDoe")

"~Home"and "JaneDoe"are just strings, right? There is no condition in the code return Redirectwhere “JaneDoe” will be what is passed to the “Redirect”, is there? I'm not sure what to do with this fragment, I can only assume that this is the place to come later.

This code is a .NET-MVC project in development that is part of a file .csthat is a LoginController.

+3
source share
5 answers

Yes, this is just bad code. It will always be equivalent

return Redirect("~/Home");

, , , . , , LHS .

+7

? , . , ; .

JaneDoe

+3

, . "~/Home", , - .

+2

, "JaneDoe".

+1

This will always use a string "~/Home". The null-coalescing (??) operator selects the left side if it is not equal to zero, or the right side if it is equal to zero.

+1
source

Source: https://habr.com/ru/post/1783670/


All Articles