TDD with .NET - implicit conversions

By: Johnathon Wright on: December 19, 2008

Continuing the "TDD in .NET":http://blog.mustmodify.com/search/label/TDD%20with%20.NET series...

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

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.



Back