加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

.net – 在VS 2005中解决没有lambda的问题

发布时间:2020-12-17 07:25:34 所属栏目:百科 来源:网络整理
导读:我从以下一些精彩的遗留代码中摘录了以下内容: Private Sub SomeMethod() Dim deductibles As List(Of Integer) = GetDeductibles() deductibles.RemoveAll(AddressOf LessThanMinDed)EndSubPrivate Function LessThanMinDed(ByVal i As Integer) As Boolea
我从以下一些精彩的遗留代码中摘录了以下内容:

Private Sub SomeMethod()
    Dim deductibles As List(Of Integer) = GetDeductibles()    
    deductibles.RemoveAll(AddressOf LessThanMinDed)
EndSub
Private Function LessThanMinDed(ByVal i As Integer) As Boolean
    Return i < MinimumDeductible()
End Function

如果你是一个语言势利小人,我们可以这样写:

private void SomeMethod() {
    List<int> deductibles = GetDeductibles();    
    deductibles.RemoveAll(LessThanMinDed);
}
private bool LessThanMinDed(int i) {
    return i < MinimumDeductible();
}

MinimumDeductible()进行数据库调用.有没有办法写这个没有写像x = MinimumDeductible():RemoveAll(Function(i)i< x)(因为lambdas不在这个版本的VB.NET中)将调用数据库就一次? 解决了(有点): 像这样解决:

Public Class Foo
    Private CachedMinimum As Integer
    Private Sub SomeMethod()
        Dim deductibles As List(Of Integer) = GetDeductibles()
        Me.CachedMinimum = MinimumDeductible()
        deductibles.RemoveAll(AddressOf LessThanMinDed)
    End Sub
    Private Function LessThanMinDed(ByVal i As Integer) As Boolean
        Return i < CachedMinimum
    End Function
End Class

解决方法

答案真的取决于语言.在C#2中,我们没有lambda表达式,但我们确实有匿名方法…所以你可以写:

List<int> deductibles = GetDeductibles();    
deductibles.RemoveAll(delegate(int i) { return i < MinimumDeductible(); });

据我所知,VS 2005附带的VB版本没有相应的版本.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读