TDD with .NET - implicit conversions

Continuing the TDD in .NET series...

 namespace EduTest{
    [TestFixture]
    public class NameTests
    {
        [Test]
        public void name_can_be_implicitly_converted_to_string()
        {
            Name mick = new Name();
            mick.FirstName = "Mick";
            mick.LastName = "Jagger";
            string micks_name = mick;
            Assert.Equals("Mick Jagger", micks_name);
         }
    }
}

This won't compile. It complains that "Cannot implicityly convert from type 'Edu. Name' to 'string'. We can go ahead and call this a failure.

namespace Edu{
    public class Name
    {
        public static implicit operator string(Name n)
        {
            return n.ToString();
        }
   }
}

Success. Yeah.

Post a comment


(lesstile enabled - surround code blocks with ---)