Skip to content

Instantly share code, notes, and snippets.

@makomweb
Last active August 29, 2015 13:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save makomweb/9207431 to your computer and use it in GitHub Desktop.
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)
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