Default value for generics

前端 未结 3 2062
温柔的废话
温柔的废话 2020-12-03 21:20

How do I create the default for a generic in VB? in C# I can call:

T variable = default(T);
  1. How do I do this in VB?
  2. If this
相关标签:
3条回答
  • 2020-12-03 21:31

    Question 1:

    Dim variable As T
    ' or '
    Dim variable As T = Nothing
    ' or '
    Dim variable As New T()
    

    Notice that the latter only works if you specify the Structure constraint for the generic type (for reference types, New T() in VB does something else than default(T) in C#).

    Question 2:

    For value types all members of the struct are “nulled” out, i.e. all reference type members are set to null (Nothing) and all value types are in turn nulled out.

    And no, since string is a reference type, it does not result in "" for strings as suggested in the other answer.

    Question 3:

    No, there's no way to specify this. There are some threads about this on Stack Overflow already, e.g. here. Jon has posted an excellent explanation why this is.

    0 讨论(0)
  • 2020-12-03 21:32

    Not really being a VB guy, I made some mistakes in my original answer that Konrad set me straight on. The original answer is below, but I wanted to update my answer to be correct based on Konrad's input.

    As Konrad says, default(T) and Nothing are in fact equivalent for both value and reference types. The correct VB code should be as follows in which case you would get the exact same behavior you get in my C# code:

    Function ReturnSomething(Of T)() As T
        Return Nothing
    End Function
    
    Function DoSomething(Of T)()
        Dim x as T = Nothing;
        If x = Nothing Then
            Console.WriteLine("x is default.")
        Else
            Console.WriteLine("x has a value.")
        End If
    

    Original (WRONG) Answer

    It looks like there is no VB equivalent to default(T). However according to this post, unlike in C#, if T is a value-type, in VB you can still use Nothing which is semantically the same in most cases.

    The big place where you would have a problem using Nothing where you would normally use default(T) is if you need to test for it inside your code. Consider the following C# code:

    T ReturnSomething<T>()
    {
        return default(T);
    }
    
    void DoSomething<T>()
    {
        T x = default(T);
        if(x == default(T))
            Console.WriteLine("x is default.");
        else
            Console.WriteLine("x has a value.");
    }
    

    Translated to VB like this:

    Function ReturnSomething(Of T)() As T
        Return Nothing
    End Function
    
    Function DoSomething(Of T)()
        Dim x as T = Nothing;
        If x Is Nothing Then
            Console.WriteLine("x is default.")
        Else
            Console.WriteLine("x has a value.")
        End If
    End Function
    

    If T is a reference type, both versions will act exactly the same for both ReturnSomething and DoSomething. However, if T is a value type, ReturnSomething will act exactly the same for either language, but DoSomething will print "x is default." in the C# version, but "x has a value." in the VB version.

    0 讨论(0)
  • 2020-12-03 21:49

    Actually folks the correct way of doing this is to cast the null (Nothing) type as your generic type as follows:

    Dim tmpObj As T = CType(Nothing, T)
    

    If you want to return the default value for the generic you simply return CType(Nothing, T)

    0 讨论(0)
提交回复
热议问题