Default value for generics

前端 未结 3 2061
温柔的废话
温柔的废话 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: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)

提交回复
热议问题