C# Convert ToBoolean(DateTime)

Description

Convert ToBoolean(DateTime) calling this method always throws InvalidCastException.

Syntax

Convert.ToBoolean(DateTime) has the following syntax.


public static bool ToBoolean(
  DateTime value
)

Parameters

Convert.ToBoolean(DateTime) has the following parameters.

  • value - The date and time value to convert.

Returns

Convert.ToBoolean(DateTime) method returns This conversion is not supported. No value is returned.

Example

The following example demonstrates that an attempt to convert a DateTime value to a Boolean type throws InvalidCastException.


//  w ww.ja v a2 s .c om
using System;
public class MainClass{
  public static void Main(String[] argv){  
    DateTime dateTime = DateTime.Now;
    
    bool boolVal;
    
    // System.InvalidCastException is always thrown. 
    try {
      boolVal = System.Convert.ToBoolean(dateTime);
    }
    catch (System.InvalidCastException) {
      System.Console.WriteLine("Conversion from DateTime to " +
        "Boolean is not supported by the .NET Framework.");
    }
  }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version