How to use object?

  • Thread starter Thread starter Andrus
  • Start date Start date
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 ?
 
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.
 
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.
 
Back
Top