Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implicit conversion from Nullable to normal, any ideas?

I want some idea to how implicitly convert nullable "?" variables to district ones.

given this example

int? x = 5;

int y = x; //this gonna fail, !!!

i need some way to override = parameter, but unfortunately the = parameter is not overloadable... any suggestions

I'm using C#

like image 801
Mustafa Magdy Avatar asked Apr 25 '11 22:04

Mustafa Magdy


4 Answers

You have two options, access the value directly (if you know for sure it's not null):

int y = x.Value;

or, use the null coalescing operator:

int y = x ?? 0; // 0 if null...
like image 79
John Rasch Avatar answered Nov 14 '22 23:11

John Rasch


It is possible to implement an implicit cast operator, but only to or from types you define. For example, doing something like this..

public class NullableExtensions 
{
    public static implicit operator int(int? value)
    {
        return value ?? default(int);
    }
}

.. will return a CS0556 compile error because the cast doesn't include the user-defined type.

The closest you could do is define your own Nullable type that does contain an implicit cast operator:

public struct ImplicitNullable<T> where T: struct
{
    public bool HasValue { get { return this._value.HasValue; } }
    public T Value { get { return this._value.Value; } }

    public ImplicitNullable(T value) : this() { this._value = value; }
    public ImplicitNullable(Nullable<T> value) : this() { this._value = value; }

    public static implicit operator ImplicitNullable<T>(T value) { return new ImplicitNullable<T>(value); }
    public static implicit operator ImplicitNullable<T>(Nullable<T> value) { return new ImplicitNullable<T>(value); }

    public static implicit operator T(ImplicitNullable<T> value) { return value._value ?? default(T); }
    public static implicit operator Nullable<T>(ImplicitNullable<T> value) { return value._value; }

    private Nullable<T> _value { get; set; }

    // Should define other Nullable<T> members, especially 
    // Equals and GetHashCode to avoid boxing
}

Note that although it's possible to write this code, it will likely lead to hard to trace bugs. I would recommend using an explicit cast, or throwing an exception when the value is null.

Afterwards, you can cast to and from as expected:

static void Main()
{
    int myInt = 1;
    int? nullableInt = 2;

    ImplicitNullable<int> implicitInt;

    // Convert from int or int?
    implicitInt = myInt;
    implicitInt = nullableInt;

    // Convert to int or int?
    myInt = implicitInt;
    nullableInt = implicitInt;
}
like image 20
Scott Wegner Avatar answered Nov 14 '22 22:11

Scott Wegner


Wait, I'm so confused...

Why don't you just use GetValueOrDefault?

like image 28
user541686 Avatar answered Nov 14 '22 22:11

user541686


I'm assuming this is C#.

You need to either cast, or use .value:

 int? x = 5;
 int y;

 if(x.HasValue)
     y = x.Value;
 else
     throw new//... handle error or something
like image 27
davidtbernal Avatar answered Nov 14 '22 21:11

davidtbernal