How can i get 0 as integer value from (int)null
.
EDIT 1: I want to create a function that will return me default values for null representa
You can use the default keyword to get the default value of any data type:
int x = default(int); // == 0
string y = default(string); // == null
// etc.
This works with generic parameters as well:
Bar Foo() {
return new Bar(default(T));
}
In case you have a variable of type object
that may contain null
or a value of type int
, you can use nullable types and the ?? operator to convert it safely to an integer:
int a = 42;
object z = a;
int b = (int?)z ?? 0; // == 42
int c = (int?)null ?? 0; // == 0