Is there any way to define a Generic in VB.NET which will accept only Singles or Doubles?
I tried the following things, based on what I've found online, but none compile:
Dim target As Nullable(Of {Single, Double})
Dim target As Nullable(Of T As {Single, Double})
Dim target As Nullable(Of T {Single, Double})
Dim target As Nullable(Of Single, Double)
Dim target As Nullabl开发者_运维技巧e(Of T As Single, Double)
I want to specify that target
can either be a Single?
or a Double?
only.
Public Class NullableFloat(Of T As {Double, Single})
is the right syntax, see here under constraints.
However, this statement specifies that the generic type T
must implement both Double
and Single
, not either. Since .Net primitives are sealed and there is no inheritance relationship between Double
and Single
there is no way that T
could ever satisfy both constraints.
Since no .Net type can be both a Double
and a Single
you could make some complex type called NullableFloat
with one Value
property of Double
type but with setters that take Double
or Single
but, why not just use Nullable(Of Double)
.
精彩评论