Csharp/C#教程:将算法从C#转换为VB.NET失败分享


将算法从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表达式中删除副作用)。

编辑:

与您的代码的差异:

上述就是C#学习教程:将算法从C#转换为VB.NET失败分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/949833.html

(0)
上一篇 2021年11月18日
下一篇 2021年11月18日

精彩推荐