Nullable<T>.ToString Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Returns the text representation of the value of the current Nullable<T> object.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Overrides Function ToString As String
public override string ToString()

Return Value

Type: System.String
The text representation of the value of the current Nullable<T> object if the HasValue property is true, or an empty string ("") if the HasValue property is false.

Remarks

The ToString property returns the string yielded by calling the ToString property of the object returned by the Value property.

Examples

The following code example displays the value of the current Nullable<T> object.

' This code example demonstrates the 
' Nullable<T>.ToString method.


Class Example
   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim nullableDate As Nullable(Of DateTime)
      ' Display the current date and time.
      nullableDate = DateTime.Now
      Display(outputBlock, "1)", nullableDate)

      ' Assign null (Nothing in Visual Basic) to nullableDate, then 
      ' display its value.
      nullableDate = Nothing
      Display(outputBlock, "2)", nullableDate)
   End Sub 'Main

   '  Display the text representation of a nullable DateTime.
   Public Shared Sub Display(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal title As String, _
                             ByVal dspDT As Nullable(Of DateTime))
      Dim msg As String = dspDT.ToString()

      outputBlock.Text += String.Format("{0} ", title)
      If String.IsNullOrEmpty(msg) Then
         outputBlock.Text &= "The nullable DateTime has no defined value." & vbCrLf
      Else
         outputBlock.Text += String.Format("The current date and time is {0}.", msg) & vbCrLf
      End If
   End Sub 'Display 
End Class 'Sample

'This code example produces the following results:
'
'1) The current date and time is 4/19/2005 8:28:14 PM.
'2) The nullable DateTime has no defined value.
'
// This code example demonstrates the 
// Nullable<T>.ToString method.

using System;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      DateTime? nullableDate;

      // Display the current date and time.
      nullableDate = DateTime.Now;
      Display(outputBlock, "1)", nullableDate);

      // Assign null (Nothing in Visual Basic) to nullableDate, then 
      // display its value.
      nullableDate = null;
      Display(outputBlock, "2)", nullableDate);
   }

   // Display the text representation of a nullable DateTime.
   public static void Display(System.Windows.Controls.TextBlock outputBlock, string title, DateTime? dspDT)
   {
      string msg = dspDT.ToString();

      outputBlock.Text += String.Format("{0} ", title);
      if (String.IsNullOrEmpty(msg))
         outputBlock.Text += "The nullable DateTime has no defined value." + "\n";
      else
         outputBlock.Text += String.Format("The current date and time is {0}.", msg) + "\n";
   }
}

/*
This code example produces the following results:

1) The current date and time is 4/19/2005 8:28:14 PM.
2) The nullable DateTime has no defined value.

*/

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.