I was asked in an interview how to return Null from a method with integer, double type, etc, without any output parameter.
You can do something like this.
You have to make int type nullable first. by using int?
normally the int datatype in c# are not nullable by default so you have to explicitly convert the int //not nullable type to int? //nullable
You can do the same thing with double etc..
// the return-type is int?. So you can return 'null' value from it.
public static int? method()
{
return null;
}
You can also write the above method in this way:
// this is another way to convert "non-nullable int" to "nullable int".
public static Nullable<int> method()
{
return null;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With