Last active
December 5, 2017 13:53
-
-
Save CumpsD/42d70c52c6973736489e353edd84c4fd to your computer and use it in GitHub Desktop.
Implicit operator problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal class Bla | |
{ | |
public int Blaat { get; } | |
public Bla(int blaat) => Blaat = blaat; | |
public static implicit operator int(Bla x) => x.Blaat; | |
} | |
internal class Bleh | |
{ | |
public Bla Bla { get; } | |
public Bleh(Bla bla) => Bla = bla; | |
} | |
internal class Program | |
{ | |
private static void Main(string[] args) | |
{ | |
var a = new Bleh(new Bla(42)); | |
int? aa = a.Bla; | |
var b = new Bleh(null); | |
int? bb = b.Bla; // This explodes because it calls the implicit operator, but the inner value is null | |
return; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment