Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将此C比较翻译为VB.NET_C#_.net_Arrays_Vb.net_Code Translation - Fatal编程技术网

C# 将此C比较翻译为VB.NET

C# 将此C比较翻译为VB.NET,c#,.net,arrays,vb.net,code-translation,C#,.net,Arrays,Vb.net,Code Translation,我正在尝试转换两个NullableOf Byte对象之间的比较: 在线代码翻译器为我提供了以下等价物: Private m_header As Nullable(Of Byte)() Public Property header() As Nullable(Of Byte)() Get Return m_header End Get Private Set(value As Nullable(Of Byte)

我正在尝试转换两个NullableOf Byte对象之间的比较:

在线代码翻译器为我提供了以下等价物:

    Private m_header As Nullable(Of Byte)()
    Public Property header() As Nullable(Of Byte)()
        Get
            Return m_header
        End Get
        Private Set(value As Nullable(Of Byte)())
            m_header = value
        End Set
    End Property

    Public Overrides Function Equals(other As Object) As Boolean

        ' ... more code translated
        If Me.header <> otherType.header Then
            Return False
        End If
        ' ... more code translated

    End Function

看起来转换器添加了偏移量是因为某些只有开发人员知道的原因,或者甚至可能不是开发人员知道的原因。当您将其放在文章末尾时,它应该可以工作

If Not Me.header Is otherType.header Then
    Return False
End If
或者,我认为这更具可读性:

If Me.header IsNot otherType.header Then
    Return False
End If

错误消息将确切地告诉您要执行的操作:

If Not Me.header Is otherType.header Then
    Return False
End If
VB.NET中的Is运算符进行引用比较,与C中未重写它的引用类型的==完全相同。字节[]就是这样一种类型。

您可以使用将空字节视为0:


当你比较2个头时,你认为什么是相等的?如果它们是完全相同的实例,或者它们包含完全相同的值,那么您可以使用If Not ReferenceEqualsMe.header、otherType.header,但是知识更丰富的VB.NET程序员无疑会想出更好的答案。@RB。C在这里不做值比较,它只是检查引用等式。@LasseV.Karlsen我知道-我想知道作者是否知道!如果您试图在不了解这两种语言的习惯用法的情况下盲目地转换代码,那么您将遇到大量问题。你为什么要皈依?为什么不直接引用程序集呢?您是否需要更改/维护代码?谢谢,但请澄清您的解决方案是否与Jon和Rondles的答案基本相同?如果不是,您的解决方案有优势?很抱歉,但由于我缺乏处理字节的知识,我无法注意到此解决方案是否比其他解决方案有优势。@ElektroStudios:Jon检查两个对象是否是相同的引用,我检查两个对象是否相等。我的方法有一个问题,它将null字节视为0,因此0将与Nothing相同。我将编辑我的答案以显示更好的方法。@ElektroStudios:我已经编辑了我的答案。我还没有注意到它是一个可为空的字节数组。很抱歉,“偏移量”部分是一个错误类型,是我的错,谢谢你的帮助
If Me.header IsNot otherType.header Then
    Return False
End If
If Not Me.header Is otherType.header Then
    Return False
End If
Public Overrides Function Equals(other As Object) As Boolean
    ' ... '
    Dim otherType = TryCast(other, ActualType)
    If otherType Is Nothing Then Return False
    If If(Me.header, 0) <> If(otherType.header, 0) Then
        Return False
    End If
    ' ... '
End Function
If header Is Nothing AndAlso otherType.header Is Nothing Then
    Return True
ElseIf Object.ReferenceEquals(header, otherType.header) Then
    Return True
ElseIf header Is Nothing OrElse otherType.header Is Nothing Then
    Return False
Else
    Return header.SequenceEqual(otherType.header)
End If