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'

后端 未结 5 1270
谎友^
谎友^ 2020-12-04 11:28

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\'\"?

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-04 12:04

    Use string instead of string? in all places in your code.

    The Nullable type requires that T is a non-nullable value type, for example int or DateTime. Reference types like string can already be null. There would be no point in allowing things like Nullable so it is disallowed.

    Also if you are using C# 3.0 or later you can simplify your code by using auto-implemented properties:

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

提交回复
热议问题