将算法从C#转换为VB.NET失败

我正在尝试将以下算法从C#转换为VB.NET,而我所拥有的VB.NET并没有产生与我的C#算法相同的结果,有人可以告诉我在转换中出错了吗?

public static IEnumerable Combinations(this IEnumerable elements, int k) { List result = new List(); // single combination if (k == 0) { result.Add(new T[0]); } else { int current = 1; foreach (T element in elements) { //combine each element with k-1 combinations of subsequent elements result.AddRange(elements .Skip(current++) .Combinations(k - 1) .Select(combination => (new T[] { element }).Concat(combination).ToArray()) ); } } return result; } 

这是我在VB.NET中得到的:

  Public Function Combinations(Of T)(ByRef elements As IEnumerable(Of T), ByVal k As Integer) As IEnumerable(Of T()) Dim result As New List(Of T())() 'single combination' If k = 0 Then result.Add(New T(-1) {}) Else Dim current As Integer = 0 For Each element As T In elements 'combine each element with k - 1 combinations of subsequent elements' Dim local As T = element result.AddRange(elements.Skip(current = current + 1).Combinations(k - 1).Select(Function(combs) (New T() {local}).Concat(combs).ToArray())) Next End If Return result End Function 

有些事情是错的,但我不确定是什么,我猜这个问题是在lambda的某个地方。

任何人都可以指出我的转换错误吗?

使用代码转换器……

  _ Public Shared Function Combinations(Of T)(elements As IEnumerable(Of T), k As Integer) As IEnumerable(Of T()) Dim result As New List(Of T())() ' single combination If k = 0 Then result.Add(New T(-1) {}) Else Dim current As Integer = 1 For Each element As T In elements 'combine each element with k-1 combinations of subsequent elements result.AddRange(elements.Skip(System.Math.Max(System.Threading.Interlocked.Increment(current),current - 1)).Combinations(k - 1).[Select](Function(combination) (New T() {element}).Concat(combination).ToArray())) Next End If Return result End Function 

我根本不是VB的专家,但问题可能源于:

current = current + 1current++ 。 它(基本上)与++current相同。 这些有不同的行为。

由于VB不直接支持内联后增量运算符,我能想到的最接近无错误的实现是(在C#中,因为我不知道VB):

 int current = 0; Func getCurrentThenIncrement = () => { int previous = current; current = current + 1; return previous; }; // ... .Skip(getCurrentThenIncrement()) 

或者,它可能源于:

 Public Function Combinations(Of T)(ByRef elements ... 

当我使用.Net Reflector来查看它并将其“转换”为VB时, elements似乎是ByVal

根据我的意见,根本不需要就地增加电流。 只需在Linq表达式递增它。 另一方面,您应该使用1初始化current ,与C#中的相同。

此外,你的“基本情况”有点奇怪; 你可以这样写:

 result.Add(New T() { }) 

不需要-1

我手边没有编译器来检查,但我会像这样转换它:

  Public Function Combinations(Of T)(elements As IEnumerable(Of T), k As Integer) As IEnumerable(Of T()) Dim result As New List(Of T())() ' single combination If k = 0 Then result.Add(New T() {}) Else Dim current As Integer = 1 For Each element As T In elements 'combine each element with k-1 combinations of subsequent elements result.AddRange(elements .Skip(PostfixInc(current)) .Combinations(k - 1) .Select(Function(combination) (New T() { element }).Concat(combination).ToArray()) ) Next End If Return result End Function Private Function PostfixInc(ByRef value As Integer) As Integer Dim currentValue = value value += 1 Return currentValue End Function 

这就像我现在想到的那样直接转换每个语法元素。 一旦你有了它,然后考虑清理它(例如尝试从LINQ表达式中删除副作用)。

编辑:

与您的代码的差异:

  • New T(-1) {}应该是New T() {}
  • current应初始化为1而不是0。
  • Skip(current = current + 1)将无法执行您想要的操作。 事实上,它什么都不做,因为current = current + 1是一个布尔表达式,总是会计算为false。 如果禁用选项严格,VB.NET将静默地将false转换为0,如果启用了选项严格,则抛出编译器错误。