C# 类型“字符串”必须是不可为空的类型,以便将其用作泛型类型或方法“System.Nullable<T>”中的参数 T
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9236468/
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
The type 'string' must be a non-nullable type in order to use it as parameter T in the generic type or method 'System.Nullable<T>'
提问by MiscellaneousUser
Why do I get Error "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'"?
为什么我会收到错误“类型‘字符串’必须是不可为空的值类型才能将其用作泛型类型或方法‘System.Nullable’中的参数‘T’”?
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using Universe;
namespace Universe
{
public class clsdictionary
{
private string? m_Word = "";
private string? m_Meaning = "";
string? Word {
get { return m_Word; }
set { m_Word = value; }
}
string? Meaning {
get { return m_Meaning; }
set { m_Meaning = value; }
}
}
}
采纳答案by Mark Byers
Use stringinstead of string?in all places in your code.
在代码中的所有位置使用string而不是string?。
The Nullable<T>type requires that T is a non-nullable value type, for example intor DateTime. Reference types like stringcan already be null. There would be no point in allowing things like Nullable<string>so it is disallowed.
该Nullable<T>类型要求 T 是不可为空的值类型,例如intor DateTime。像这样的引用类型string已经可以为空。允许这样的事情Nullable<string>被禁止是没有意义的。
Also if you are using C# 3.0 or later you can simplify your code by using auto-implemented properties:
此外,如果您使用的是 C# 3.0 或更高版本,则可以使用自动实现的属性来简化代码:
public class WordAndMeaning
{
public string Word { get; set; }
public string Meaning { get; set; }
}
回答by Jon Skeet
stringis a reference type, a class. You can only use Nullable<T>or the T?C# syntactic sugar with non-nullable valuetypes such as intand Guid.
string是一个引用类型,一个类。您只能将Nullable<T>或T?C# 语法糖用于不可为空的值类型,例如int和Guid。
In particular, as stringis a reference type, an expression of type stringcan already be null:
特别是,作为string引用类型,类型的表达式string已经可以为空:
string lookMaNoText = null;
回答by Joshua Enfield
For a very specific reason Type Nullable<int>put your cursor on Nullable and hit F12 - The Metadata provides the reason (Note the struct constraint):
出于非常具体的原因,请键入Nullable<int>将光标放在 Nullable 上并按 F12 - 元数据提供了原因(注意结构约束):
public struct Nullable<T> where T : struct
{
...
}
回答by Chagbert
System.String(with capital S) is already nullable, you do not need to declare it as such.
System.String(大写 S)已经可以为空,您无需如此声明。
(string? myStr)is wrong.
(string? myStr)是错的。
回答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 defaultand 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.
这里重要的是显示代码的意图。如果“意图”是引用类型可以为空,则对其进行标记,否则将空值分配给不可为空会导致编译器警告。

