Tuesday, July 9, 2013

a cleaner version of my last blog posting

Considering this:

using System.Collections.Generic;
namespace MyApplication
{
   public static class YieldExample
   {
      public static IEnumerable<decimal> Go()
      {
         decimal[] distances = new decimal[] { 67.8M, 0.7M, 6.001M, 63.3M };
         foreach (decimal distance in distances)
         {
            yield return distance;
         }
      }
   }
}

 
 

Test like so:

using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace MyApplication.Tests
{
   [TestClass]
   public class YieldExampleTest
   {
      [TestMethod]
      public void TestMethod()
      {
         IEnumerable<decimal> decimals = YieldExample.Go();
         List<decimal> list = decimals.ToList();
         Assert.AreEqual(list[0], 67.8M);
         Assert.AreEqual(list[1], 0.7M);
         Assert.AreEqual(list[2], 6.001M);
         Assert.AreEqual(list[3], 63.3M);
      }
   }
}

No comments:

Post a Comment