Last active
August 29, 2015 13:56
-
-
Save makomweb/9207431 to your computer and use it in GitHub Desktop.
C# implicit type conversion (see http://msdn.microsoft.com/en-us/library/zk2z37d3.aspx for a more detailed explanation)
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
public class ImplicitConversionTest | |
{ | |
public class A | |
{ | |
public string Member { get; set; } | |
public static implicit operator string(A self) | |
{ | |
return self.Member; | |
} | |
} | |
public class B | |
{ | |
public B(string value) | |
{ | |
Member = value; | |
} | |
public string Member { get; private set; } | |
} | |
[Test] | |
public void Test() | |
{ | |
var a = new A { Member = "foo"}; | |
var b = new B(a); // a will be implicitly converted into a string! | |
Assert.That(b.Member, Is.EqualTo(a.Member)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment