【问题标题】: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>'类型“字符串”必须是不可为空的类型,才能将其用作泛型类型或方法“System.Nullable<T>”中的参数 T
【发布时间】:2012-03-03 10:26:35
【问题描述】:

为什么我会收到错误“类型 'string' 必须是不可为空的值类型才能将其用作泛型类型或方法 '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; }
      }
    }
}

【问题讨论】:

  • String 已经可以为空。
  • 我认为这可能会回答您的问题:C# nullable string error。一位用户对两者都发布了相同的答案,并且两个副本都已被投票。

标签: c# nullable


【解决方案1】:

在代码中的所有位置都使用string 而不是string?

Nullable&lt;T&gt; 类型要求 T 是不可为空的值类型,例如 intDateTimestring 之类的引用类型已经可以为空。允许Nullable&lt;string&gt; 之类的东西是没有意义的,所以它是不允许的。

此外,如果您使用 C# 3.0 或更高版本,您可以使用 auto-implemented properties 来简化代码:

public class WordAndMeaning
{
    public string Word { get; set; }
    public string Meaning { get; set; }
}

【讨论】:

  • M.Babcock,当我执行 m_Word = null 时,它会出错,有什么建议吗?我希望能够将 Word 设置为 null。
  • @MiscellaneousUser:你得到什么错误信息?您可以发布您尝试编译的确切文件吗?只看到一行代码很难猜出你的错误是什么。我的意思是你可能错过了一个分号......但也许你只是忘记复制+粘贴它......这只是猜测,直到你发布你试图编译的代码。
  • 感谢帮助,看到了stackoverflow.com/questions/187406/…这个贴子,可以看到问号只针对值类型。
  • @MiscellaneousUser:这不仅仅是“用于值类型”。它必须明确地是一个 non-nullable 值类型。正如错误消息所说。
  • 嘿,在 Swift 编程了一段时间之后,这个在 C# 项目中得到了我最好的。
【解决方案2】:

string 是一个引用类型,一个类。您只能将Nullable&lt;T&gt;T? C# 语法糖与不可为空的 类型(例如intGuid)一起使用。

特别是,由于string 是一个引用类型,string 类型的表达式可以已经为空:

string lookMaNoText = null;

【讨论】:

    【解决方案3】:

    出于一个非常具体的原因,键入 Nullable&lt;int&gt; 将光标放在 Nullable 上并按 F12 - 元数据提供了原因(注意结构约束):

    public struct Nullable<T> where T : struct
    {
    ...
    }
    

    http://msdn.microsoft.com/en-us/library/d5x73970.aspx

    【讨论】:

    • 请注意,尽管 Nullable&lt;int&gt; 是一个结构,但 Nullable&lt;Nullable&lt;int&gt;&gt; 是不允许的。
    • 这很有趣。那是“硬编码”到编译器中吗?它如何受特定结构的约束(Nullable>)? - 编辑我现在看到它显然很特别 - 编译错误...必须是不可为空的值类型......
    【解决方案4】:

    System.String(大写 S)已经可以为空,你不需要这样声明。

    (string? myStr) 错误。

    【讨论】:

    • 对您的答案进行小修改,以突出大写字母的重要性。我是 C# 的新手,我花了很长时间才得到这个小小的区别。
    • 这个说法不正确 string 是 System.String 的别名,它们是一样的
    【解决方案5】:

    请注意,在即将发布的 C# 版本 8 中,答案不正确。

    All the reference types are non-nullable by default 你实际上可以执行以下操作:

    public string? MyNullableString; 
    this.MyNullableString = null; //Valid
    

    然而,

    public string MyNonNullableString; 
    this.MyNonNullableString = null; //Not Valid and you'll receive compiler warning. 
    

    这里重要的是显示代码的意图。 如果“意图”是引用类型可以为空,则标记它,否则将空值分配给不可为空的将导致编译器警告。

    More info

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多