C#可为空的字符串错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/187406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# nullable string error
提问by Mike Fielden
private string? typeOfContract
{
get { return (string?)ViewState["typeOfContract"]; }
set { ViewState["typeOfContract"] = value; }
}
Later in the code I use it like this:
稍后在代码中我像这样使用它:
typeOfContract = Request.QueryString["type"];
I am getting the following error at the declaration of typeOfContract
line stating:
我在声明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>'
类型“字符串”必须是不可为空的值类型,以便将其用作泛型类型或方法“System.Nullable<T>”中的参数“T”
Any ideas? Basically, I want to make sure that "type"
exists in the QueryString
before performing an action.
有任何想法吗?基本上,我想确保在执行操作之前"type"
存在QueryString
。
采纳答案by Joe
System.String is a reference type and already "nullable".
System.String 是一种引用类型并且已经“可为空”。
Nullable<T> and the ? suffix are for value types such as Int32, Double, DateTime, etc.
Nullable<T> 和 ? 后缀用于值类型,例如 Int32、Double、DateTime 等。
回答by jop
You are making it complicated. string
is already nullable. You don't need to make it morenullable. Take out the ?
on the property type.
你让它变得复杂。string
已经可以为空。你不需要让它更可空。去掉?
属性类型上的。
回答by Szymon Rozga
string cannot be the parameter to Nullable because string is not a value type. String is a reference type.
string 不能作为 Nullable 的参数,因为 string 不是值类型。字符串是一种引用类型。
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 askeyword.
由于as关键字,应该可以工作。
回答by csgero
String is a reference type, so you don't need to (and cannot) use Nullable<T>
here. Just declare typeOfContract as string and simply check for null after getting it from the query string. Or use String.IsNullOrEmpty if you want to handle empty string values the same as null.
String 是一种引用类型,因此您不需要(也不能)Nullable<T>
在此处使用。只需将 typeOfContract 声明为字符串,并在从查询字符串中获取它后简单地检查是否为 null。或者,如果要处理与 null 相同的空字符串值,请使用 String.IsNullOrEmpty。
回答by James Oravec
For nullable, use ?
with all of the C# primitives, except for string.
对于可为空的,?
与所有C# 原语一起使用,字符串除外。
The following page gives a list of the C# primitives: http://msdn.microsoft.com/en-us/library/aa711900(v=vs.71).aspx
以下页面提供了C# 原语列表:http: //msdn.microsoft.com/en-us/library/aa711900( v= vs.71).aspx
回答by ANewGuyInTown
Please note that in upcoming version of C# which is 8, the answers are not true.
请注意,在即将发布的 C# 版本 8 中,答案是不正确的。
All the reference types are non-nullable by default
and you can actually do the following:
All the reference types are non-nullable by default
您实际上可以执行以下操作:
public string? MyNullableString;
this.MyNullableString = null; //Valid
However,
然而,
public string MyNonNullableString;
this.MyNonNullableString = null; //Not Valid and you'll receive compiler warning.
The important thing here is to show the intent of your code.If the "intent" is that the reference type can be null, then mark it so otherwise assigning null value to non-nullable would result in compiler warning.
这里重要的是显示代码的意图。如果“意图”是引用类型可以为空,则对其进行标记,否则将空值分配给不可为空会导致编译器警告。
To the moderator who is deleting all the answers, don't do it. I strongly believe this answer adds value and deleting would simply keep someone from knowing what is right at the time. Since you have deleted all the answers, I'm re-posting answer here. The link that was sent regarding "duplicates" is simply an opening of some people and I do not think it is an official recommendation.
对于正在删除所有答案的版主,请不要这样做。我坚信这个答案会增加价值,而删除只会让某人不知道当时什么是正确的。由于您已删除所有答案,因此我将在此处重新发布答案。发送的关于“重复”的链接只是一些人的开场白,我认为这不是官方推荐。