【问题标题】:Converting to VB.NET from C# yield [duplicate]从 C# 转换为 VB.NET 产量 [重复]
【发布时间】:2013-08-06 10:27:19
【问题描述】:

我知道 VB.NET 没有 yield 关键字,那么你将如何转换枚举的 yield。在下面的代码中?

    private static IEnumerable<int> Combinations(int start, int level, int[] arr)
    {
        for (int i = start; i < arr.Length; i++)
            if (level == 1)
                yield return arr[i];
            else
                foreach (int combination in Combinations(i + 1, level - 1, arr))
                    yield return arr[i] * combination;
    }

编辑:这适用于 .NET 2.0

有什么想法吗?

谢谢,

【问题讨论】:

标签: c# vb.net yield


【解决方案1】:

当前版本的 VB.Net 确实支持 yield 关键字。我使用了自动转换from here 来生成这段代码。

Private Shared Function Combinations(start As Integer, level As Integer, arr As Integer()) As IEnumerable(Of Integer)
    For i As Integer = start To arr.Length - 1
        If level = 1 Then
            yield Return arr(i)
        Else
            For Each combination As Integer In Combinations(i + 1, level - 1, arr)
                yield Return arr(i) * combination
            Next
        End If
    Next
End Function

如果您不能使用 yield,您需要一个列表来存储结果,然后在循环结束时返回它。例如:

Private Shared Function Combinations(start As Integer, level As Integer, arr As Integer()) As IEnumerable(Of Integer)
    Dim result As New List(Of Integer)()
    For i As Integer = start To arr.Length - 1
        If level = 1 Then
            result.Add(arr(i))
        Else
            For Each combination As Integer In Combinations(i + 1, level - 1, arr)
                result.Add(arr(i) * combination)
            Next
        End If
    Next
    Return result
End Function

【讨论】:

  • 对不起,这是针对 VB.NET 2.0 的,它不支持 Yield 关键字,所以它不起作用。
  • 显然不是您正在使用的版本,但它确实有效:msdn.microsoft.com/en-us/library/hh156729.aspx 尽管我添加了一个变体来展示您如何在没有 yield 的情况下做到这一点
  • 与自动转换的代码一样,结果是错误的(转换器显然已经过时并且不支持 VB.Net 的迭代器块)。首先,函数必须用Iterator 关键字标记:Private Shared Iterator Function Combinations。此外,它只是yield &lt;expression&gt;,而不是yield return &lt;expression&gt;
【解决方案2】:

解决方案在这个问题中:Yield in VB.NET

如上所述,这个关键字现在是 VB 的一部分,如果您使用的是旧版本的 VB.Net,请查看链接。

【讨论】:

    【解决方案3】:

    我在网上找到的例子是

    C#代码

    public class List
    {
    //using System.Collections; 
    public static IEnumerable Power(int number, int exponent)
    {
        int counter = 0;
        int result = 1;
        while (counter++ < exponent)
        {
            result = result * number;
            yield return result;
        }
    }
    
    static void Main()
    {
        // Display powers of 2 up to the exponent 8: 
        foreach (int i in Power(2, 8))
        {
            Console.Write("{0} ", i);
        }
    }
    }
    

    /* 输出: 2 4 8 16 32 64 128 256 */

    与 Visual C# 示例给出相同结果的 VB.Net 代码。

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Form1
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
        Dim results As System.Collections.Generic.IEnumerable(Of Integer)
        results = Power(2, 8)
    
        Dim sb As New System.Text.StringBuilder
        For Each num As Integer In results
            sb.Append(num.ToString & " ")
        Next
    
        MessageBox.Show(sb.ToString)
    
    End Sub
    
    Public Function Power(ByVal number As Integer, ByVal exponent As Integer) As System.Collections.Generic.IEnumerable(Of Integer)
    
        Dim result As New List(Of Integer)
    
        For index As Integer = 1 To exponent
            result.Add(Convert.ToInt32(Math.Pow(number, index)))
        Next
        Return result.AsEnumerable
    
    End Function
    
    End Class
    

    参考 --> http://msdn.microsoft.com/en-us/library/9k7k7cf0(v=vs.90).aspx

    【讨论】:

    • 这让我很困惑。为什么这与问题有关?
    • 好像您没有阅读参考网页。那里清楚地提到了。你的困惑应该被清除。
    • 您的示例与原始帖子无关。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-04
    • 1970-01-01
    • 2011-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-14
    相关资源
    最近更新 更多