Skip to content

Instantly share code, notes, and snippets.

@ChrisMissal
Created April 10, 2014 18:47
Show Gist options
  • Save ChrisMissal/10411127 to your computer and use it in GitHub Desktop.
Save ChrisMissal/10411127 to your computer and use it in GitHub Desktop.
Implicit operator causing sneaky NullReferenceException
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