【问题标题】:C# unable to create class having Nullable<string>C# 无法创建具有 Nullable<string> 的类
【发布时间】:2013-11-14 15:12:59
【问题描述】:

我是 C# 新手,如有错误请纠正我
我正在尝试创建一个具有 Nullable 属性的类,但它在构建我的类看起来像的类时出错

public class vwAcdAdmissionWithvwAcdAdmissionSessionDetailWithAllMaster1
    {
        public Nullable<string> Name { get; set; }
        public Nullable<string> StudentNameWithLedgerName { get; set; }
        public Nullable<decimal> AASDid { get; set; }
        public Nullable<decimal> StudentLedgerId { get; set; }
        public Nullable<string> LedgerName { get; set; }
        public Nullable<decimal> SessionId { get; set; }
        public Nullable<string> ScholarNo { get; set; }
        public Nullable<decimal> ClassId { get; set; }
        public Nullable<decimal> SectionId { get; set; }
        public Nullable<decimal> MediumId { get; set; }
        public Nullable<decimal> StreamId { get; set; }
        public Nullable<decimal> HouseId { get; set; }
        public Nullable<decimal> FeeCategoryId { get; set; }
        public Nullable<decimal> SchemeId { get; set; }
        public Nullable<decimal> RollNo { get; set; }
        public Nullable<decimal> SchoolId { get; set; }
        public Nullable<decimal> UserId { get; set; }
        public Nullable<decimal> ShiftId { get; set; }
        public Nullable<string> StreamName { get; set; }
        public Nullable<string> ClassName { get; set; }
        public Nullable<string> HouseName { get; set; }
        public Nullable<string> SectionName { get; set; }
        public Nullable<string> ShiftName { get; set; }
        public Nullable<string> MediumName { get; set; }
        public Nullable<string> FeeCategoryName { get; set; }
        public Nullable<string> DiscountType { get; set; }
        public Nullable<decimal> Discount { get; set; }
}

错误是错误是
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&lt;T&gt;'
我不明白这是什么意思,我现在能做什么
请建议我,并感谢您的宝贵意见


我想让我的字符串可以为空的原因类似于我以前的帖子 last post
返回不可为空的字符串时会出错

【问题讨论】:

  • 这是一个巨大的类名:)

标签: c# .net class nullable


【解决方案1】:

string 已经是 nullable 类型,因为它是一个类。您只能将Nullable&lt;T&gt;structs 一起使用。

Nullable&lt;T&gt;的源码:

public struct Nullable<T> where T : struct

string 绝对不满足这里的通用约束。

【讨论】:

  • +1,但是你不需要去源码。 MSDN is clear enough.
  • @Co.Aden 这个答案有什么不清楚的地方?字符串已经可以为空,实际上在 C# 中不存在不可为空的字符串。因此,只需将您的字符串属性声明为常规字符串,但您可以将小数属性保留为 Nullable&lt;decimal&gt;
  • 是的,我明白这个字符串不像可以为空的字符串
【解决方案2】:

消息说明了它的含义:只有结构(值类型)可以在Nullable&lt;T&gt;中使用。

因为string(或System.String)是Reference Type - 而且肯定不是导致编译器错误的结构! - 那么这种(引用)类型的表达式固有地允许null作为一个值,并且不需要用Nullable包装它们。

要“修复”此问题,只需将所有 Nullable&lt;string&gt; x 更改为 string x

【讨论】:

  • 我编辑了我的帖子,请告诉我现在应该做什么
  • @Co.Aden 不确定发生了什么变化。 All Nullable&lt;string&gt; 应该被替换,以便它甚至可以编译。如果还有其他问题(与此编译器错误无关),请确保创建一个新问题。
【解决方案3】:

string 是一种引用类型,用于 Nullable T 的 for 必须是值类型。 http://msdn.microsoft.com/en-us/library/362314fe(v=vs.90).aspx
http://msdn.microsoft.com/en-us/library/vstudio/1t3y8s4s.aspx

无论如何你都可以使用string s = null,所以不需要Nullable&lt;string&gt;

【讨论】:

    【解决方案4】:

    这里已经有很多正确答案了,但是由于 OP 似乎仍然有些混乱,我想尽可能简单明了地说明这一点:

    您应该将Nullable&lt;something&gt;something 一起使用,而something 本身的值不能为Null。以DateTime 为例 - 它的默认值为DateTime.MinValue(表示01.01.01 的常量),但永远 不能有Null 的值。 int 也是如此;它永远不能保持null的值

    也就是说,你不能这样做:

    DateTime invalidDate = null;
    

    但你可以做到:

    Nullable<DateTime> validDate = null;
    

    字符串,另一方面,可以为空,所以你基本上不需要可以为空:

    string justFine = null;
    

    因此,尝试使用Nullable&lt;string&gt; 毫无意义,而这基本上就是解决此问题所需要知道的。

    【讨论】:

      【解决方案5】:

      decimal 是一个值类型。 string 是一个类。 Null 不能分配给 ValueType 的变量,除非您将它们包装在 Nullable&lt;&gt; 中。 Null 已经可以分配给像 string 这样的类的变量。

      【讨论】:

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