Created
May 4, 2012 07:47
-
-
Save mastoj/2592970 to your computer and use it in GitHub Desktop.
Implicit operator demo
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
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