欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

c#3.0实现延迟赋值示例

时间:2022-07-17 09:13:40|栏目:.NET代码|点击:

延迟赋值主要有两点:

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;
}

上一篇:C#/VB.NET 在Word中添加条码、二维码的示例代码

栏    目:.NET代码

下一篇:详解LINQ入门(中篇)

本文标题:c#3.0实现延迟赋值示例

本文地址:http://www.codeinn.net/misctech/208041.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有