程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> c#3.0完成延遲賦值示例

c#3.0完成延遲賦值示例

編輯:C#入門知識

c#3.0完成延遲賦值示例。本站提示廣大學習愛好者:(c#3.0完成延遲賦值示例)文章只能為提供參考,不一定能成為您想要的結果。以下是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>
///Laziestheexpressiontest.
///</summary>
[TestCase]
publicvoidLazyExpressionTest()
{
varlme1=newLazyExpression<int>(()=>2+1);
varlme2=newLazyExpression<int>(()=>1);
Assert.AreEqual(4,Add(lme1,lme2));
}

///<summary>
///Addsthespecifiedx.
///</summary>
///<paramname="x">Thex.</param>
///<paramname="y">They.</param>
///<returns>result</returns>
privateintAdd(LazyExpression<int>x,LazyExpression<int>y)
{
returnx.Evaluate()+y.Evaluate();
}

///<summary>
///Laziestheexpressionwithlogic.
///</summary>
[TestCase]
publicvoidLazyExpressionWithLogic()
{
varexp1=newLazyExpression<bool>(()=>true);
varexp2=newLazyExpression<bool>(()=>true||false);
if(LazyBoolExpression.And(exp1,exp2))
{
Console.WriteLine("lazyand");
}
if(LazyBoolExpression.Or(exp1,exp2))
{
Console.WriteLine("lazyor");
}
}

///<summary>
///Laziesthememoizedexpressiontest.
///</summary>
[TestCase]
publicvoidLazyMemoizedExpressionTest()
{
varlme1=newLazyMemoizedExpression<int>(()=>GetIntResult());
Assert.AreEqual(943,lme1.Evaluate());
Assert.AreEqual(943,lme1.Evaluate());
//output:
//1passed,0failed,0skipped,took2.80seconds(NUnit2.5).
}

///<summary>
///Comparestolazyexpressiontest.
///</summary>
[TestCase]
publicvoidCompareToLazyExpressionTest()
{
varlme1=newLazyExpression<int>(()=>GetIntResult());
Assert.AreEqual(943,lme1.Evaluate());
Assert.AreEqual(943,lme1.Evaluate());
//output:
//1passed,0failed,0skipped,took4.80seconds(NUnit2.5).
}

///<summary>
///Getstheintresult.
///</summary>
///<returns></returns>
privateintGetIntResult()
{
//currentthreadsleeptwosecond.
System.Threading.Thread.Sleep(2000);
return943;
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved