Strings can always be null, you don't need to try and make it nullable as they're not value types. Just remove the ? That makes it nullable to fix it.
Writing Nullable value with BinaryWriter [duplicate]
-
04-04-2022 - |
Pregunta
I want to write a nullable string to file using BinaryWriter
and this code:
BinaryWriter writer = new BinaryWriter(st);
String? s;
if(s!=null){
writer.Write(s);
}
but this error occurs:
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'
How can I solve this problem?
Solución
Otros consejos
Strings are already nullable in C#.NET, they're a reference type with syntactic sugar around them to make them easier to use. You can't make a nullable double nullable, the universe would collapse.
According to MSDN documentation, we can see that Nullable type can be used only with structs:
[SerializableAttribute]
public struct Nullable<T>
where T : struct
p.s. see also Constraints on Type Parameters.
String is a reference type. It can not be nullable.
public struct Nullable<T> where T : struct, new()
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow