Every value has an associated type in .NET Framework , which defines attributes such as the amount of space allocated to the value, the range of possible values it can have maximum and the members that it makes available .

what is System.InvalidCastException

What is Type Conversion?

Type conversion is converting one type of data to another type. This means that, it creates a value in a new type that is equivalent to the value of an old type, but it does not necessarily preserve the exact value (or identity) of the original type. It is also known as Type Casting .

Microsoft .NET Framework automatically supports the following type conversions:
  1. A derived class to a base class.
  2. A base class back to the original derived class.
  3. A type that implements an interface to an interface object that represents that interface.
  4. An interface object back to the original type that implements that interface.

What is InvalidCastException?

An InvalidCastException is thrown when cast from one type to another type is not supported. In some reference type conversions , the compiler cannot determine whether a cast will be valid. It is because of the source type cannot be converted to the destination type , so the cast does not succeed.

For example, if you try to convert a Char value to a DateTime value throws an InvalidCastException exception.

Why do I get this InvalidCastException Android

It is important to note that this exception is caused by programming error and should not be handled in a try/catch block; instead, the cause of the exception should be eliminated.

class Program { static void Main(string[] args) { object obj = new Object(); int i; try { i = (int)obj; } catch (InvalidCastException e) { Console.WriteLine(e); } } }
System.InvalidCastException: Specified cast is not valid.

How to fix InvalidCastException?

System.InvalidCastException is caused by programming error and should not be handled in a try/catch block; instead, the cause of the exception should be eliminated.

is operator

C# provides the "is" operator to enable you to test for compatibility before actually performing a cast operation . It will checks if the runtime type of an expression result is compatible with a given type. The "is operator" returns true if the given object is of the same type otherwise, return false. It also returns false for null objects . It's a straightforward extension of the "is" statement that enables concise type evaluation and conversion.

expr is type

Where expr is an expression that returns a value and type is the name of a type or a type parameter. The argument expr cannot be an anonymous method or a lambda expression .

class Program { static void Main(string[] args) { int i = 100; object obj = i; Console.WriteLine(obj is int); // output: True Console.WriteLine(obj is long); // output: False } }

ToString() method

class Program { static void Main(string[] args) { object obj = 100; try { string str = (string)obj; } catch (InvalidCastException e) { Console.WriteLine(e); } } }

In the above example, you are trying to convert a value or an object to its string representation by using a casting operator . When you execute the program you will get " System.InvalidCastException : Unable to cast object of type 'System.Int32' to type 'System.String'."

The ToString() method is defined by the Object class and therefore is either inherited or overridden by all managed types. So, here we call its ToString method because it successfully covert an instance of any type to its string representation .

class Program { static void Main(string[] args) { object obj = 100; string str = obj.ToString(); Console.WriteLine(str); Console.ReadKey(); } }

C# type conversion

Property Description
HelpLink Gets or sets a link to the help file associated with this exception.
HResult Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception.
StackTrace Gets a string representation of the frames on the call stack at the time the current exception was thrown.

Method Description
Equals Determines whether the specified object is equal to the current object.
GetType Gets the runtime type of the current instance.
ToString Creates and returns a string representation of the current exception.