Nullable<(Of <(T>)>).ToString Method

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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

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

Syntax

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<(Of <(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<(Of <(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

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Platforms

Windows Phone

See Also

Reference

Nullable<(Of <(T>)>) Structure

System Namespace