What is the purpose of a question mark after a type (for example: int? myVariable)?

前端 未结 8 2062
盖世英雄少女心
盖世英雄少女心 2020-11-22 10:26

Typically the main use of the question mark is for the conditional, x ? \"yes\" : \"no\".

But I have seen another use for it but can\'t find an explanat

相关标签:
8条回答
  • 2020-11-22 11:10

    practical usage:

    public string someFunctionThatMayBeCalledWithNullAndReturnsString(int? value)
    {
      if (value == null)
      {
        return "bad value";
      }
    
      return someFunctionThatHandlesIntAndReturnsString(value);
    }
    
    0 讨论(0)
  • 2020-11-22 11:11

    It is a shorthand for Nullable<int>. Nullable<T> is used to allow a value type to be set to null. Value types usually cannot be null.

    0 讨论(0)
提交回复
热议问题