C#可空字符串错误 [英] C# nullable string error

查看:200
本文介绍了C#可空字符串错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

后来在code我用这样的:

Later in the code I use it like this:

typeOfContract = Request.QueryString["type"];

我收到以下错误在typeOfContract行的声明,说明

I am getting the following error at the declaration of typeOfContract line stating

类型字符串必须是一个非空的值类型才能使用
  它作为参数的泛型类型或方法T
  System.Nullable

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'

任何想法?基本上,我希望确保类型,在查询字符串中存在执行行动之前。

Any ideas? Basically, I want to make sure that "type" exists in the queryString before performing an action.

推荐答案

System.String是引用类型和已经可为空。

System.String is a reference type and already "nullable".

&可空LT; T>和?后缀是值类型,如的Int32,双,日期时间等。

Nullable<T> and the ? suffix are for value types such as Int32, Double, DateTime, etc.

这篇关于C#可空字符串错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆