Skip to content

Instantly share code, notes, and snippets.

@mastoj
Created May 4, 2012 07:47
Show Gist options
  • Save mastoj/2592970 to your computer and use it in GitHub Desktop.
Save mastoj/2592970 to your computer and use it in GitHub Desktop.
Implicit operator demo
class Program
{
static void Main(string[] args)
{
var helloB = new HelloB() {PropB = "PropB"};
Console.WriteLine("HelloB.PropB: " + helloB.PropB);
HelloA helloA = helloB;
Console.WriteLine("HelloA.PropA: " + helloA.PropA);
helloA.PropA = "PropA";
Console.WriteLine("HelloA.PropA: " + helloA.PropA);
HelloC helloC = helloA;
Console.WriteLine("HelloC.PropC: " + helloC.PropC);
Console.ReadLine();
}
}
class HelloA
{
public string PropA { get; set; }
public static implicit operator HelloA(HelloB other)
{
return new HelloA { PropA = other.PropB };
}
public static implicit operator HelloB(HelloA other)
{
return new HelloB { PropB = other.PropA };
}
public static implicit operator HelloA(HelloC other)
{
return new HelloA { PropA = other.PropC };
}
public static implicit operator HelloC(HelloA other)
{
return new HelloC { PropC = other.PropA };
}
}
class HelloB
{
public string PropB { get; set; }
}
class HelloC
{
public string PropC { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment