C# 可空字符串错误

标签 c# nullable

private string? typeOfContract
{
  get { return (string?)ViewState["typeOfContract"]; }
  set { ViewState["typeOfContract"] = value; }
}

稍后在代码中我这样使用它:

typeOfContract = Request.QueryString["type"];

我在声明 typeOfContract 行时遇到以下错误:

The type 'string' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable<T>'

有什么想法吗?基本上,我想在执行操作之前确保 "type" 存在于 QueryString 中。

最佳答案

System.String 是一个引用类型并且已经“可为空”。

可空 和 ?后缀用于值类型,例如 Int32、Double、DateTime 等。

关于C# 可空字符串错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/187406/

相关文章:

c# - 如何在功能运行时更新表单

c# - Matrix3D 入门 (Silverlight)

c# - 如何阻止 Crystal Report Viewer 在打开子报表时询问登录凭据

c# - 什么时候int?和 Nullable<int> 是相同的,为什么它们的行为方式不同?

c# - 可空类型到底什么时候抛出异常?

android - 将 kotlin 转换为 java 后,Notnull 变量变为可为空

c# - 在 Linq 表达式中使用可空类型

即使使用 System.Linq,C# FirstOrDefault() 也无法正常工作

c# - 无法将 int 转换为可空 int

c# - 绑定(bind)到模型或 View 模型