Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.