Created
April 10, 2014 18:47
-
-
Save ChrisMissal/10411127 to your computer and use it in GitHub Desktop.
Implicit operator causing sneaky NullReferenceException
This file contains hidden or 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
void Main() | |
{ | |
var order = new Order | |
{ | |
Instructions = SpecialInstructions.LeaveAtDoor, | |
}; | |
var thing = new Thing | |
{ | |
Instructions = order.Instructions, | |
}; | |
thing.Dump(); | |
} | |
// Define other methods and classes here | |
public class Order | |
{ | |
public SpecialInstructions Instructions {get; set;} | |
} | |
public class Thing | |
{ | |
public string Instructions { get; set; } | |
} | |
public class SpecialInstructions | |
{ | |
public static SpecialInstructions LeaveAtDoor = new SpecialInstructions(1, "Leave at door"); | |
private SpecialInstructions(int value, string displayName) | |
{ | |
Value = value; | |
DisplayName = displayName; | |
} | |
public int Value {get; private set;} | |
public string DisplayName {get; private set;} | |
public static implicit operator string(SpecialInstructions value) | |
{ | |
return value.DisplayName; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment