English 中文(简体)
在VB,C# s`default'等值。 NET?
原标题:What is the equivalent of C# s `default` in VB.NET?

I m 通常在C#的家中,I m 在VB中研究业绩问题。 NET代码: 我想能够比较某种类型的缺省值(例如C# sdefault关键词)。

public class GenericThing<T1, T2>
{
    public T1 Foo( T2 id )
    {
        if( id != default(T2) ) // There doesn t appear to be an equivalent in VB.NET for this(?)
        {
            // ...
        }
    }
}

I was led to believe that Nothing was semantically the same, yet if I do:

Public Class GenericThing(Of T1, T2)
    Public Function Foo( id As T2 ) As T1
        If id IsNot Nothing Then
              ...
        End If
    End Function
End Class

然后在<代码>T2上<>Integer<>/code>,id0, 仍须通行证和 >的正体得到评价。 然而,如果我这样做的话:

    Public Function Bar( id As Integer ) As T1
        If id <> Nothing Then
              ...
        End If
    End Function

当时情况没有得到满足,没有评估尸体。

最佳回答

这不是一个完整的解决办法,因为原来的C#代码没有汇编。 你们可以通过地方变量使用任何东西:

Public Class GenericThing(Of T)
    Public Sub Foo(id As T)
        Dim defaultValue As T = Nothing
        If id <> defaultValue Then
            Console.WriteLine("Not default")
        Else
            Console.WriteLine("Default")
        End If
    End Function
End Class

这并不像C#版本那样编纂成册,你可以比较像这种未经约束的类型参数。

您可使用<条码> 平等Comparer(Of T),但即使如此,您也不一定需要当地变量:

If Not EqualityComparer(Of T).Default.Equals(id, Nothing) Then
问题回答

不同于C#, VB。 NET并不要求用一种表达方式来初步形成一个当地变量。 该公司在运行时间开始达到违约值。 仅凭您作为default的替代品所需要的内容。 关键词:

    Dim def As T2       Get the default value for T2
    If id.Equals(def) Then
          etc...
    End If

不要忘记这一评论,而是要让一些人走胡?? a 从现在起一年。

您的法典中的问题是Is Not营运人,而不是Nothing。 http://msdn.microsoft.com/en-us/library/t3bat82c%28v=VS.90%29.aspx”rel=“noreferer”

The IsNot operator determines if two object references refer to different objects. However, it does not perform value comparisons.

你试图与参考操作者进行价值比较。 一旦你们认识到这一点,Joon Skeet s或Hans Passant的答案就成为显而易见的解决办法。





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签