1

I want to create a class like Nullable except for the moment that it could work with classes and structures:

public class MyWrapper<T>
{
    public MyWrapper(T value)
    {
        Value = value;
    }

    public T Value { get; set; }
}

Then I want to add implicit conversion of Nullable to MyWrapper:

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

And of course, it fails because of Nullable restrictions:

Only non-nullable value type could be underlying of 'System.Nullable'

It is a pretty understandable error but in theory, I could convert any Nullable to MyWrapper, because of restrictions of Nullable harder than MyWrapper's restrictions.

So it there any workaround for Nullable to MyWrapper implicit conversion?


Why do I need to use MyWrapper for classes?

We use some sort of bad GraphQl on the backend and sent updation objects like this:

class UpdateProductRequest
{   
    public MyWrapper<string> Country {get;set;}

    public MyWrapper<string> Title {get;set;}
}

So

new UpdateProductRequest
{
    Title = "new title"
}

update the title, but not the country.

  • 2
    Aren't classes inherently nullable? I'm not sure I understand what you're trying to do. – Daxtron2 Apr 03 '18 at 12:27
  • @RenéVogt, Yes you right. I have fixed the code. Thanks! – Rustam Salakhutdinov Apr 03 '18 at 12:28
  • @TJWolschon, yes they are. We use MyWrapper wrapper for some special purpose to distinguish the null MyWrapper and the MyWrapper with null. We understand should we modify the value to null (if MyWrapper has null value) or should skip correspond value at all (if MyWrapper is null itself) – Rustam Salakhutdinov Apr 03 '18 at 12:31
  • I don't think there is a good way to solve this. The compiler needs to enforce the restrictions on `Nullable`. So the only other way would be to create a generic operator (with a second type argument for the nullable), but [generic operators also are not supported in c#](https://stackoverflow.com/questions/5905563/c-sharp-generic-operators) – René Vogt Apr 03 '18 at 12:36
  • @RenéVogt. yes I know that generic operators are not supported( – Rustam Salakhutdinov Apr 03 '18 at 12:38

1 Answers1

1

You can define implicit casting from underlying type to MyWrapper:

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

Now with such operator you can do this:

MyWrapper<int> w = new int?(5);
MyWrapper<int> w2 = (int?)null; //here casting method is not called

So initially the Nullable is casted down to the underlying type and then to MyWrapper. If it's null there is just null assignment to variable of typeMyWrapper which is class so it's valid. Nullable has special treatment by the compiler so it may look like a magic but it works.

arekzyla
  • 2,878
  • 11
  • 19
  • Firstly, I have warned about such case: MyWrapper myVal = (int?)null; Null -> T will raise the exception and everything goes down. But you know what? It works and you will have null MyWrapper in this case. Can you explain such behavior? It is great for my goal and a little be unexpected for me. – Rustam Salakhutdinov Apr 03 '18 at 13:00
  • 1
    @RustamSalahutdinov There is no casting there to `MyWrapper` - the operator's method is not called. It's the same as writing `MyWrapper myVal = null;` – arekzyla Apr 03 '18 at 13:08