What do two question marks together mean in C#?

前端 未结 18 1330
借酒劲吻你
借酒劲吻你 2020-11-22 03:41

Ran across this line of code:

FormsAuth = formsAuth ?? new FormsAuthenticationWrapper();

What do the two question marks mean, is it some ki

18条回答
  •  迷失自我
    2020-11-22 04:11

    If you're familiar with Ruby, its ||= seems akin to C#'s ?? to me. Here's some Ruby:

    irb(main):001:0> str1 = nil
    => nil
    irb(main):002:0> str1 ||= "new value"
    => "new value"
    irb(main):003:0> str2 = "old value"
    => "old value"
    irb(main):004:0> str2 ||= "another new value"
    => "old value"
    irb(main):005:0> str1
    => "new value"
    irb(main):006:0> str2
    => "old value"
    

    And in C#:

    string str1 = null;
    str1 = str1 ?? "new value";
    string str2 = "old value";
    str2 = str2 ?? "another new value";
    

提交回复
热议问题