C#3.0贯彻延迟赋值

   延迟赋值主要有两点:

   1.一个参数可能或可能没被赋值.

 2.一个参数在一个函数中老是使用时或许被赋值.

如下边的那种场地:

int Add(int x, int y)
{
    return (2 + 1) + (1);
}

动用Func<T>,大家轻松落成,看代码:

/// <summary>
/// LazyExpression
/// </summary>
/// <typeparam name="T">T</typeparam>
public class LazyExpression<T>
{
    Func<T> thunk;
    public LazyExpression(Func<T> Thunk)
    {
        thunk = Thunk;
    }
    public T Evaluate()
    {
        return thunk();
    }
}

/// <summary>
/// LazyBoolExpression
/// </summary>
public static class LazyBoolExpression
{
    public static bool And(LazyExpression<bool> LHS, LazyExpression<bool> RHS)
    {
        return LHS.Evaluate() && RHS.Evaluate();
    }
    public static bool Or(LazyExpression<bool> LHS, LazyExpression<bool> RHS)
    {
        return LHS.Evaluate() == true ? true : RHS.Evaluate();
    }
}

/// <summary>
/// LazyMemoizedExpression
/// </summary>
/// <typeparam name="T"></typeparam>
public class LazyMemoizedExpression<T>
{
    bool thunked;
    T value;
    Func<T> thunk;
    public LazyMemoizedExpression(Func<T> Thunk)
    {
        thunked = false;
        thunk = Thunk;
    }
    public T Evaluate()
    {
        if (!thunked)
        {
            value = thunk();
            thunked = true;
        }
        return value;
    }
}

LazyExpression<T>类完毕了着力的推移赋值,LazyMemoizedExpression<T>类完成了一回赋值,数十次使用.
LazyBoolExpression完毕逻辑表明式. 

看UnitTest,一切就通晓了.

     /// <summary>
     /// Lazies the expression test.
     /// </summary>
     [TestCase]
     public void LazyExpressionTest()
     {
         var lme1 = new LazyExpression<int>(() => 2 + 1);
         var lme2 = new LazyExpression<int>(() => 1);
         Assert.AreEqual(4, Add(lme1, lme2));   
     }

     /// <summary>
     /// Adds the specified x.
     /// </summary>
     /// <param name="x">The x.</param>
     /// <param name="y">The y.</param>
     /// <returns>result</returns>
     private int Add(LazyExpression<int> x, LazyExpression<int> y)
     {
         return x.Evaluate() + y.Evaluate();
     }

     /// <summary>
     /// Lazies the expression with logic.
     /// </summary>
     [TestCase]
     public void LazyExpressionWithLogic()
     {
        var exp1 = new LazyExpression<bool>(() => true);
         var exp2 = new LazyExpression<bool>(() => true || false);
         if (LazyBoolExpression.And(exp1, exp2))
         {
             Console.WriteLine("lazy and");
         }
         if (LazyBoolExpression.Or(exp1, exp2))
         {
             Console.WriteLine("lazy or");
         }
     }

     /// <summary>
     /// Lazies the memoized expression test.
     /// </summary>
     /// <remarks>Author Petter Liu http://wintersun.cnblogs.com </remarks>
     [TestCase]
     public void LazyMemoizedExpressionTest()
     {
         var lme1 = new LazyMemoizedExpression<int>(() => GetIntResult());
         Assert.AreEqual(943, lme1.Evaluate());
         Assert.AreEqual(943, lme1.Evaluate());
         //output:
         //1 passed, 0 failed, 0 skipped, took 2.80 seconds (NUnit 2.5).
     }

     /// <summary>
     /// Compares to lazy expression test.
     /// </summary>
     /// <remarks>Author Petter Liu http://wintersun.cnblogs.com </remarks> 
     [TestCase]
     public void CompareToLazyExpressionTest()
     {
         var lme1 = new LazyExpression<int>(() => GetIntResult());
         Assert.AreEqual(943, lme1.Evaluate());
         Assert.AreEqual(943, lme1.Evaluate());
         //output:
         //1 passed, 0 failed, 0 skipped, took 4.80 seconds (NUnit 2.5).
     }

     /// <summary>
     /// Gets the int result.
     /// </summary>
     /// <returns></returns>
     private int GetIntResult()
     {
         //current thread sleep two second.
         System.Threading.Thread.Sleep(2000);
         return 943;
     }

 

Author:Petter Liu     http://wintersun.cnblogs.com

相关文章