What is the equivalent of C#'s `default` in VB.NET?

前端 未结 3 1971
粉色の甜心
粉色の甜心 2021-01-01 16:53

I\'m normally at home in C#, and I\'m looking at a performance issue in some VB.NET code -- I want to be able to compare something to the default value for a type (kind of l

3条回答
  •  余生分开走
    2021-01-01 17:06

    Unlike C#, VB.NET doesn't require a local variable to be initialized with an expression. It gets initialized to its default value by the runtime. Just what you need as a substitute for the default keyword:

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

    Don't forget the comment, it is going to make somebody go 'Huh?' a year from now.

提交回复
热议问题