C# nullable string error

前端 未结 6 1956
梦如初夏
梦如初夏 2020-11-27 05:19
private string? typeOfContract
{
  get { return (string?)ViewState[\"typeOfContract\"]; }
  set { ViewState[\"typeOfContract\"] = value; }
}

Later

6条回答
  •  伪装坚强ぢ
    2020-11-27 05:52

    string cannot be the parameter to Nullable because string is not a value type. String is a reference type.

    string s = null; 
    

    is a very valid statement and there is not need to make it nullable.

    private string typeOfContract
        {
          get { return ViewState["typeOfContract"] as string; }
          set { ViewState["typeOfContract"] = value; }
        }
    

    should work because of the as keyword.

提交回复
热议问题