How to use object?

A

Andrus

public static bool Empty(object? obj) {

return false;
}

public static bool Empty(string? obj) {

return false;
}


causes error

Error 1 The type 'object' must be a non-nullable value type in order to use
it as parameter 'T' in the generic type or method 'System.Nullable<T>'


How to use nullable objects and strings ?
 
N

Nicholas Paldino [.NET/C# MVP]

Andrus,

There is no such thing as a nullable object or a nullable string. The
nullable indicator can only be applied to value types.

Object and string are both reference types. They are able to be set to
null by default.

Hope this helps.
 
B

Bruce Wood

public static bool Empty(object? obj) {

return false;
}

public static bool Empty(string? obj) {

return false;
}

causes error

Error 1 The type 'object' must be a non-nullable value type in order to use
it as parameter 'T' in the generic type or method 'System.Nullable<T>'

How to use nullable objects and strings ?

Objects and strings are by their nature nullable: they're reference
types. saying "string?" is the same as saying "string", which is why
the compiler won't let you do it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top