ChatGPT解决这个技术问题 Extra ChatGPT

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>'

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

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 is already nullable.
I think this might answer your question: C# nullable string error. A user has posted identical answers to both, and both copies have been upvoted.

M
Mark Byers

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

The Nullable<T> 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<string> 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; }
}

M.Babcock, when I do m_Word = null , it errors, any suggestions? I want to be able to set Word to be null.
@MiscellaneousUser: What error message do you get? Can you post the exact file that you tried to compile? It's hard to guess what your mistake is from seeing only one line of your code. I mean it could be that you are missing a semi-colon... but maybe you just forgot to copy+paste it... It's nothing but guesswork until you post the code you tried to compile.
Thanks for the help, saw this posting stackoverflow.com/questions/187406/… and can see that the question mark is only for value types.
@MiscellaneousUser: It's not just "for value types". It must specifically be a non-nullable value type. Just as the error message says.
Heh, after programming Swift for a while, this one got the best of me on a C# project.
J
Jon Skeet

string is a reference type, a class. You can only use Nullable<T> or the T? C# syntactic sugar with non-nullable value types such as int and Guid.

In particular, as string is a reference type, an expression of type string can already be null:

string lookMaNoText = null;

B
B--rian

System.String (with capital S) is already nullable, you do not need to declare it as such.

(string? myStr) is wrong.


Small edit to your answer, to highlight the importance of the capital letter. I am new to C# and it took me for ever to get this little difference.
this statement is untrue string is an alias for System.String, they are the same
A
ANewGuyInTown

Please note that in upcoming version of C# which is 8, the answers are not true.

All the reference types are non-nullable by default and you can actually do the following:

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.

More info


J
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):

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

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


Note though that Nullable<Nullable<int>> is disallowed even though Nullable<int> is a struct.
That's interesting. Is that "hard-coded" into the compiler? How is it constrained from a specific struct (Nullable>)? - Edit I see now apparently it is special - Compile Error...must be a non-nullable value type....