Visual Basic 中 List(Of T).ForEach 中的 Action(Of T) [英] Action(Of T) in Visual Basic in List(Of T).ForEach

查看:21
本文介绍了Visual Basic 中 List(Of T).ForEach 中的 Action(Of T)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了很多关于如何使用此功能的文档.虽然我可以编写的循环很简单而且不需要时间,但我真的很想学习如何使用它.

I have searched high and low for documentation on how to use this feature. While the loop I could write would be simple and take no time, I really would like to learn how to use this.

基本上我有一个类,比如说,Widget,带有一个 Save() 子,它什么都不返回.所以:

Basically I have a class, say, Widget, with a Save() sub that returns nothing. So:

Dim w as New Widget()
w.Save()

基本上保存小部件.现在假设我有一个通用集合 List(Of Widget) 名称 widgetList(Of Widget) 并且我想在上运行 Save()该列表中的每个项目.它说我可以做一个

basically saves the widget. Now let's say I have a generic collection List(Of Widget) name widgetList(Of Widget) and I want to run a Save() on each item in that list. It says I can do a

widgetList.ForEach([enter Action(Of T) here])

....但是在 F 中这是如何工作的???intrablag 上的任何地方都没有任何文档.非常感谢您的帮助.

....but how in the F does this work??? There is no documentation anywhere on the intrablags. Help would be much much appreciated.

推荐答案

如果您使用的是 VB9 (VS2008) 我不认为您将能够轻松使用匿名函数 -据我所知,VB9 中的匿名函数必须是真正的函数(即它们必须返回一个值)而 Action 不返回任何东西.C# 2 的匿名方法和 C# 3 的 lambda 表达式更通用,这就是为什么您会看到大量使用 C# 中的 List<T>.ForEach 的示例,而很少使用 VB :(

If you're using VB9 (VS2008) I don't think you'll be able to use an anonymous function easily - as far as I'm aware, anonymous functions in VB9 have to be real functions (i.e. they have to return a value) whereas Action<T> doesn't return anything. C# 2's anonymous methods and C# 3's lambda expressions are more general, which is why you'll see loads of examples using List<T>.ForEach from C# and very few using VB :(

您可能会编写一个 MakeAction 包装器,它接受一个 Function 并返回一个 Action,但我怀疑对 VB9 匿名函数的其他限制会使这不切实际.

You could potentially write a MakeAction wrapper which takes a Function<T,TResult> and returns an Action<T>, but I suspect other restrictions on VB9 anonymous functions would make this impractical.

好消息是 VB10 具有更多匿名功能功能支持.(C#4 和 VB10 正在获得彼此的功能 - 我相信 MS 从现在开始正试图在比以前更大的程度上实现语言平等.)

The good news is that VB10 has much more anonymous function support. (C#4 and VB10 are gaining each other's features - I believe MS is trying to go for language parity from now on, to a larger extent than before.)

在那之前,要使用 List.ForEach,您需要编写适当的 Sub 并使用 AddressOf 创建一个从它代表.这是一个小例子:

Until then, to use List<T>.ForEach you'll need to write an appropriate Sub and use AddressOf to create a delegate from it. Here's a small example:

Imports System
Imports System.Collections.Generic

Public Class Test

    Shared Sub Main()
        Dim names as New List(Of String)
        names.Add("Jon")
        names.Add("Holly")
        names.ForEach(AddressOf PrintMe)

    End Sub

    Shared Sub PrintMe(ByVal text as String)
        Console.WriteLine(text)
    End Sub

End Class

这篇关于Visual Basic 中 List(Of T).ForEach 中的 Action(Of T)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆