Cautious With Implicit Cast Operators in Structs
I find overloading operators a very powerful tool in a programmer’s arsenal, yet it seems a very rare practice among C# developers. I’ve just got bitten by a simple scenario where an explicit cast operator is a questionable idea.
In my code I have an
IriRef class. It’s basically a string or Uri wrapped in a structure, which adds semantics required
by my library JsonLd.Entities to properly serialize objects to JSON-LD. Here’s a excerpt, with
other stuff removed.
1 2 3 4 5 6 7 8
As you see it can be constructed from
Uri instances. However I don’t want to call these constructors every
time so I added two cast operators.
1 2 3 4 5 6 7 8 9 10 11 12
See the difference?
Uri can be casted implicitly so
1 2 3 4 5
I didn’t want two
implicit operator, because while ear Uri will be a valid
IriRef the same cannot be said about
simple string. Casting a string to
IriRef must be a conscious decision and the compiler will help a little.
Here’s how this decision bit me
The problem arose when I first used a
Nullable<IriRef>. The problem is that the implicit cast automatically converts
null values into into an
IriRef, which means that
IriRef? will never be null.
1 2 3 4
Very interesting lesson indeed :)