Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

public virtual int Fill(DataSetservices.Jobs_detailsDataTable dataTable, 
    global::System.Nullable<global::System.String> fromdate,     
    global::System.Nullable<global::System.DateTime> todate)

I wrote above code in dataset.xsd in C#, but it is throwing an error:

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

Suggest me how to use string because i want to use string and nothing else

like image 400
Pranav Mishra Avatar asked Sep 17 '13 14:09

Pranav Mishra


People also ask

Is string a nullable type?

The nullable type can only be used with value types and not with reference types. So, we cannot use nullable with string.

What is non-nullable value type?

Nullable variables may either contain a valid value or they may not — in the latter case they are considered to be nil . Non-nullable variables must always contain a value and cannot be nil . In Oxygene (as in C# and Java), the default nullability of a variable is determined by its type.

How do you make a string not nullable in C#?

The only way to create a non-nullable type is to declare a struct - structs, however, cannot inherit or be inherited from. Using properties as you are is most likely the best way, or null-coalescing during deserialization as previously suggested, but C# is simply designed to handle null values.

What is the purpose of a nullable type?

You typically use a nullable value type when you need to represent the undefined value of an underlying value type. For example, a Boolean, or bool , variable can only be either true or false .


1 Answers

string is already nullable, because it's a reference type. You don't need to wrap it in Nullable in order to have a null value. Not only is it not needed, but as per the error message you're getting, it's not even possible. Only non-nullable value types can be used as the generic argument for Nullable.

like image 104
Servy Avatar answered Sep 28 '22 08:09

Servy