views:

385

answers:

6

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 representation in their respective datatypes.

EDIT 2: How can i work in this scenario for using default.

(int)Value

Where Value can be null or any integer value. I dont know datatype of value at run time. But i will assure that Value should either contain null or Integer value only.

A: 

You can't cast a null to an int, as an int is a value type. You can cast it to an int? though.

What is it you want to achieve?

thecoop
@thecoop: I am trying to make a general function for returning me a default values for all the datatypes.
Shantanu Gupta
+11  A: 

You can use the Nullable structure

int value = new Nullable<int>().GetValueOrDefault();

You can also use the default keyword

int value = default(int);

Following 2nd edit:

You need a function that receive any type of parameter, so object will be used. Your function is similar to the Field<T> extension method on a DataRow

public static T GetValue<T>(object value)
{
    if (value == null || value == DBNull.Value)
        return default(T);
    else
        return (T)value;
}

Using that function, if you want an int (and you expect value to be an int) you call it like:

int result = GetValue<int>(dataRow["Somefield"]);
Pierre-Alain Vigeant
I agree. No need to reinvent the wheel - use the build it language feature - nullable types.
Kris Krause
@Pierre-Alain Vigeant: Need more explanation on "new Nullable<int>().GetValueOrDefault();" about what and how it works. I gets bit confused using <Field> dont know about this kind of syntax code
Shantanu Gupta
@ShantanuGupta A nullable type like `int?` provides a method called GetValueOrDefault(). This method returns the value if it is not null or the default value of the type (0 for an int) if the value is null.
Pierre-Alain Vigeant
@ShantanuGupta GetField<T> the T is a generic type. You can read about generics at http://msdn.microsoft.com/en-us/library/ms379564%28VS.80%29.aspx
Pierre-Alain Vigeant
@Pierre-Alain Vigeant: very nice answer given. Thx
Shantanu Gupta
Also just for the sake of completeness, default(int?) would return null as would any Nullable type as opposed to the default value of the wrapped type. So be aware that although default(int) resolves to 0 default(int?) intuitively resolves down to just null. As for whether (int?)null or default(int?) is preferable in code I'm not sure but it would be easy to do a quick test to see the resulting IL and do a quick performance test.
jpierson
+22  A: 

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<T> Foo<T>() {
    return new Bar<T>(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
dtb
+1: and you don't need to make it its own function either.
280Z28
+1 for the ?? operator.
Daniel Pryden
+1, didn't know about the default keyword.
iandisme
+1  A: 

A generic method that returns a cast instance of an object or the default value could be implemented as follows:

static T Cast<T>(object value) {
    if (value is T)
        return (T)value;
    else
        return default(T);
}

This way, using a valid value will yield the value itself:

int value = Cast<int>(4); //value = 4

and a null value will get the default:

int value = Cast<int>(null); //value = 0

Notice that since the method takes object as an argument, this will cause boxing when used with struct objects (like int).

Lck
A: 

A method of a generic class that returns nothing works:

 Class GetNull(Of T)
      Public Shared Function Value() As T
           Return Nothing
      End Function
 End Class



 Debug.Print(GetNull(Of Integer).Value())
Joshua
A: 

Check out this post for a mostly complete method for writing an extension to do just this. My scenario is pulling data from a NameValueCollection.

http://hunterconcepts.com/blog/Extensions_HttpRequestFormQueryString/

hunter