C 3 0實現延遲賦值

2021-09-05 20:56:34 字數 1811 閱讀 8061

延遲賦值主要有兩點:

1.乙個引數可能或可能沒被賦值.

2.乙個引數在乙個函式中每次使用時可能被賦值.

如下面的這種情況:

int add(int x, int y)

使用func,我們輕鬆實現,看**:

/// /// 

lazyexpression

///

///

tpublic class

lazyexpression

public t evaluate()

}/// ///

lazyboolexpression

///

public static class

lazyboolexpression

public static bool or(lazyexpression

lhs, lazyexpression

rhs)

}/// ///

lazymemoizedexpression

///

///

public class

lazymemoizedexpression

public t evaluate()

return value;

}}

lazyexpression

類實現了基本的延遲賦值,lazymemoizedexpression

類實現了一次賦值,多次使用.

lazyboolexpression實現邏輯表示式.

看unittest,一切就明白了.

/// /// 

lazies the expression test.

///

[testcase]

public void lazyexpressiontest()

/// ///

adds the specified x.

///

///

the x.

///

the y.

///

result

private int add(lazyexpression

x, lazyexpression

y)/// ///

lazies the expression with logic.

///

[testcase]

public void lazyexpressionwithlogic()

if (lazyboolexpression.or(exp1, exp2))

}/// ///

lazies the memoized expression test.

///

///

author petter liu

[testcase]

public void lazymemoizedexpressiontest()

/// ///

compares to lazy expression test.

///

///

author petter liu

[testcase]

public void comparetolazyexpressiontest()

/// ///

gets the int result.

///

///

private int getintresult()

author:petter liu    

C 3 0語法簡介

using system using system.collections.generic using system.windows.forms static class program public class testcsharp3 新特性1 prop直接出來的是簡單屬性,無需另外多寫乙個私有字...

c 3 0新增特性

c 3.0引入了乙個叫做 var 的新關鍵字。這個關鍵字允許開發者建立乙個變數,但不必明確它的型別。例如,用var說明乙個字串,就像這樣 c 3.5新特性參見 varmydata this is my data 注意,這裡並沒有提到mydata變數是乙個字串,而c 2.0卻要求這一點。雖然var允許...

C 3 0 入門系列(一)

談到dlinq,就不得不先說linq。讓我們先看看什麼是linq。linq是 language integrated query的縮寫。那麼事實上dlinq就是 database language integrated query 的縮寫。linq和dlinq最大的區別就在與linq是對記憶體進行操...