tags:

views:

319

answers:

2

What is VB's equivalent for C#'s default(T)

+6  A: 

It's any of these:

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

Assigning Nothing event to value types is perfectly fine in VB.NET. And the latter is only possible if you specify either New, or Structure constraint for the generic type.

Anton Gogolev
Reflector suggests using the following (but equivalent) line:Dim variable As T = CType(Nothing, T)
Matthew Steeples
+1 to Dim variable as T = Nothing
Pondidum
+1  A: 

The closest equivalent to default(T) is really CType(Nothing, T) since it can be used in any context that default(T) is used (i.e. as an expression).

PaulV