C#到VB.NET代码的转换

本文关键字:转换 代码 NET VB | 更新日期: 2023-09-27 18:28:31

你好,

我在将函数从C#转换为VB.NET时遇到了一个小问题

    public static IEnumerable<string> Lexicograph(List<string> characters, int length)
    {
        for (int i = 0; i < characters.Count; i++)
        {
            if (length == 1)
                yield return characters[i];
            else
                foreach (string nxt in Lexicograph(characters.GetRange(i + 1, characters.Count - (i + 1)), length - 1))
                    yield return characters[i] + " " + nxt;
        }
    }

我知道有一些在线转换器,但由于编译器说我的代码中存在错误/警告(从String转换为IEnumerable),所以它们没有正确地完成工作。

我对列表和IEnumerable接口不太熟悉,这就是我来这里的原因。

如果有人能找到这个或任何其他类型的小费的解决方案,我们将不胜感激。

提前谢谢。

C#到VB.NET代码的转换

如果您使用的是Visual Studio 2012:

Public Shared Iterator Function Lexicograph(characters As List(Of String), length As Integer) As IEnumerable(Of String)
    For i = 0 To characters.Count - 1
        If length = 1 Then
            Yield characters(i)
        Else
            For Each nxt In Lexicograph(characters.GetRange(i + 1, characters.Count - (i + 1)), length - 1)
                Yield characters(i) & " " & nxt
            Next
        End If
    Next
End Function

如果你使用的是旧版本,如果你对非懒惰变体很满意:

Public Shared Function LexicographNonLazy(characters As List(Of String), length As Integer) As IEnumerable(Of String)
    Dim result = new List(Of String)
    For i = 0 To characters.Count - 1
        If length = 1 Then
            result.Add(characters(i))
        Else
            For Each nxt In Lexicograph(characters.GetRange(i + 1, characters.Count - (i + 1)), length - 1)
                result.Add(characters(i) & " " & nxt)
            Next
        End If
    Next
    Return result
End Function

试试这个:

Public Shared Iterator Function Lexicograph(ByVal characters As List(Of String), ByVal length As Integer) As IEnumerable(Of String)
        For i As Integer = 0 To characters.Count - 1
            If length = 1 Then
                Yield characters(i)
            Else
                For Each nxt As String In Lexicograph(characters.GetRange(i + 1, characters.Count - (i + 1)), length - 1)
                    Yield characters(i) & " " & nxt
                Next nxt
            End If
        Next i
 End Function

试试这个代码。。这样想也许可以解决。。

编辑

  Public Shared Function Lexicograph(characters As List(Of String), length As Integer) As IEnumerable(Of String)
  For i As Integer = 0 To characters.Count - 1
    If length = 1 Then
        yield Return characters(i)
    Else
        For Each nxt As String In Lexicograph(characters.GetRange(i + 1, characters.Count - (i + 1)), length - 1)
            yield Return characters(i) + " " & nxt
        Next
    End If
Next
End Function

使用:代码转换器C#转换为VB