C null merge operator?? An example of the use of of double question marks

  • 2020-06-19 11:40:15
  • OfStack

The & # 63; The & # 63; Is a 2-element operator that returns the left operand if the left operand is not null, or the right operand, so it can be used instead of ? in certain situations. : Operator to simplify code writing.

Case 1:

int length = Request.QueryString["l"] != null ? int.Parse(Request.QueryString["l"]) : 0;

Using the & # 63; The & # 63; Operator:

int length = int.Parse(Request.QueryString["l"] ?? "0");

Example 2:


int? m = null;
int n = m == null ? (int)m : 0;

Using the & # 63; The & # 63; Operator:


int? m = null;
int n = m ?? 0;

Resources:

[1]. & # 63; The & # 63; Operator (C #) : http: / / msdn microsoft. com/zh - cn library/ms173224 (v = vs. 80). aspx


Related articles: