Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should implicit operators handle null?

We've got a type which has an implicit string operator. It looks like this:

public class Foo
{
    readonly string _value;

    Foo(string value)
    {
        _value = value;
    }

    public static implicit operator string(Foo foo)
    {
        return foo._value;
    }

    public static implicit operator Foo(string fooAsText)
    {
        return new Foo(fooAsText);
    }

}

We've just had a scenario where the instance passed to the implicit operator was null. Obviously, we ended up with a NullReferenceException.

I thought this was fair enough, after all, what IS the string representation of a type that is null - hard to say - so the exception seemed valid and something we shouldn't intercept/suppress/handle/ignore. My reasoning was along the lines of 'someone's given me a null, why should I return null. I don't do this in other methods'. I thought, 'I know, I'll just check for null before converting it', something like:

string s = foo == null ? null : foo;

But that didn't work, because it's now converting to a string before the comparison to null. Of course, this comparison would work:

Foo f = null;
string s;
if (f != null)
{
    s = f;
}

... but that's just ugly.

I read the section in Essential C# 5 4th edition (a 'Rough Cuts' book on Safari) and it says:

DO NOT throw exceptions from implicit conversions.

This says don't throw exceptions, but it leaves me wondering should I suppress exceptions?

The most obvious thing to do is to jump into the method and change it to:

public static implicit operator string(Foo foo)
{
    return foo == null ? null : foo._value;
}

Problem solved. But I feel dirty. I feel like I'm hiding a potential bug by checking for null. Am I being paranoid/anal/stupid?

like image 532
Steve Dunn Avatar asked Nov 29 '12 14:11

Steve Dunn


People also ask

What is an implicit operator?

The Implicit Operator According to MSDN, an implicit keyword is used to declare an implicit user-defined type conversion operator. In other words, this gives the power to your C# class, which can accepts any reasonably convertible data type without type casting.

What is null forgiving operator?

By using the null-forgiving operator, you inform the compiler that passing null is expected and shouldn't be warned about. You can also use the null-forgiving operator when you definitely know that an expression cannot be null but the compiler doesn't manage to recognize that.

Is null C# operator?

operator is known as Null-coalescing operator. It will return the value of its left-hand operand if it is not null. If it is null, then it will evaluate the right-hand operand and returns its result. Or if the left-hand operand evaluates to non-null, then it does not evaluate its right-hand operand.

What does null in C# mean?

null (C# Reference) The null keyword is a literal that represents a null reference, one that does not refer to any object. null is the default value of reference-type variables. Ordinary value types cannot be null, except for nullable value types.


1 Answers

What I would recommend in this case is that the operator which can fail should be explicit rather than implicit. The idea of an implicit conversion is that it is widening (i.e. it will never fail.) Explicit conversions, on the other hand, are understood to sometimes be able to fail.

like image 112
Dan Bryant Avatar answered Oct 01 '22 03:10

Dan Bryant