C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
HasValue: This property returns true or false. It indicates whether an underlying value is contained in the nullable.
BooleanGetValueOrDefault: This function safely returns the underlying value. If the nullable has no value, it returns the value type's default.
Value: This property will throw an exception if the nullable has no value. Prefer GetValueOrDefault to avoid exceptions.
VB.NET program that uses nullable integer
Module Module1
Sub Main()
' A nullable can be Nothing.
Dim element As Integer? = Nothing
Console.WriteLine("HASVALUE: {0}", element.HasValue)
Console.WriteLine("GETVALUEORDEFAULT: {0}", element.GetValueOrDefault())
' Use an Integer value for the nullable Integer.
element = 100
Console.WriteLine("VALUE: {0}", element.Value)
Console.WriteLine("GETVALUEORDEFAULT: {0}", element.GetValueOrDefault())
End Sub
End Module
Output
HASVALUE: False
GETVALUEORDEFAULT: 0
VALUE: 100
GETVALUEORDEFAULT: 100