﻿<?xml version="1.0" encoding="utf-8"?><Type Name="InvalidCastException" FullName="System.InvalidCastException" FullNameSP="System_InvalidCastException" Maintainer="ecma"><TypeSignature Language="ILASM" Value=".class public serializable InvalidCastException extends System.SystemException" /><TypeSignature Language="C#" Value="public class InvalidCastException : SystemException" /><TypeSignature Language="ILAsm" Value=".class public auto ansi serializable beforefieldinit InvalidCastException extends System.SystemException" /><MemberOfLibrary>BCL</MemberOfLibrary><AssemblyInfo><AssemblyName>mscorlib</AssemblyName><AssemblyPublicKey>[00 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00 ]</AssemblyPublicKey><AssemblyVersion>1.0.5000.0</AssemblyVersion><AssemblyVersion>2.0.0.0</AssemblyVersion><AssemblyVersion>4.0.0.0</AssemblyVersion></AssemblyInfo><ThreadingSafetyStatement>All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe.</ThreadingSafetyStatement><Base><BaseTypeName>System.SystemException</BaseTypeName></Base><Interfaces /><Attributes><Attribute><AttributeName>System.Runtime.InteropServices.ComVisible(true)</AttributeName></Attribute></Attributes><Docs><example><para>The following example demonstrates an error that causes
      a <see cref="T:System.InvalidCastException" /> exception.</para><code lang="C#">using System;
public class InvalidCastExample {
 public static void Main() {
   object obj = new Object();
   int i;
   try {
     i = (int) obj;
   }
   catch( InvalidCastException e ) {
     Console.WriteLine("Caught: {0}", e);
   }
 }
}
   </code><para>The output is</para><code>
Caught: System.InvalidCastException: Specified cast is not valid.
   at InvalidCastExample.Main()
			</code></example><remarks><attribution license="cc4" from="Microsoft" modified="false" /><para>An <see cref="T:System.InvalidCastException" /> exception is thrown when the conversion of an instance of one type to another type is not supported. It differs from an <see cref="T:System.OverflowException" /> exception, which is thrown when a conversion of one type to another is supported, but the value of the source type is outside the range of the target type. </para><para>For information about conversions supported by the system, see the <see cref="T:System.Convert" /> class. For errors that occur when the destination type can store source type values, but is not large enough to store a specific source value, see the <see cref="T:System.OverflowException" /> exception.</para><block subset="none" type="note"><para>In many cases, your language compiler detects that no conversion exists between the source type and the target type and issues a compiler error. </para></block><para>Some of the conditions under which an attempted conversion throws an <see cref="T:System.InvalidCastException" /> exception include the following: </para><list type="bullet"><item><para>You directly or indirectly call a primitive type's <see cref="T:System.IConvertible" /> implementation that does not support a particular conversion. For example, trying to convert a <see cref="T:System.Boolean" /> value to a <see cref="T:System.Char" /> or a <see cref="T:System.DateTime" /> value to an <see cref="T:System.Int32" /> throws an <see cref="T:System.InvalidCastException" /> exception. The following example calls both the <see cref="M:System.Boolean.System#IConvertible#ToChar(System.IFormatProvider)" /> and <see cref="M:System.Convert.ToChar(System.Boolean)" /> methods to convert a <see cref="T:System.Boolean" /> value to a <see cref="T:System.Char" />. In both cases, the method call throws an <see cref="T:System.InvalidCastException" /> exception.  </para><para>code reference: System.InvalidCastException#2</para></item><item><para>You've called the <see cref="M:System.Convert.ChangeType(System.Object,System.Type)" /> method to convert an object from one type to another, but one or both types don't implement the <see cref="T:System.IConvertible" /> interface. </para></item><item><para>Neither the source type nor the target type defines an explicit or narrowing conversion between the two types, and the <see cref="T:System.IConvertible" /> implementation of one or both types doesn't support a conversion from the source type to the target type. </para></item><item><para>You are downcasting, that is, trying to convert an instance of a base type to one of its derived types. In the following example, trying to convert a Person object to a PersonWithID object fails. </para><para>code reference: System.InvalidCastException#1</para><para>As the example shows, the downcast succeeds only if the Person object was created by an upcast from a PersonWithId object to a Person object, or if the Person object is null. </para></item></list><para>For an explicit reference conversion to be successful, the source value must be null, or the object type referenced by the source argument must be convertible to the destination type by an implicit reference conversion.</para><para>The following intermediate language (IL) instructions throw an <see cref="T:System.InvalidCastException" /> exception: </para><list type="bullet"><item><para><paramref name="castclass" /></para></item><item><para><paramref name="refanyval" /></para></item><item><para><paramref name="unbox" /></para></item></list><para><see cref="T:System.InvalidCastException" /> uses the HRESULT COR_E_INVALIDCAST, which has the value 0x80004002.</para><para>For a list of initial property values for an instance of <see cref="T:System.InvalidCastException" />, see the <see cref="M:System.InvalidCastException.#ctor" /> constructors.</para></remarks><summary><attribution license="cc4" from="Microsoft" modified="false" /><para>The exception that is thrown for invalid casting or explicit conversion.</para></summary></Docs><Members><Member MemberName=".ctor"><MemberSignature Language="ILASM" Value="public rtspecialname specialname instance void .ctor()" /><MemberSignature Language="C#" Value="public InvalidCastException ();" /><MemberSignature Language="ILAsm" Value=".method public hidebysig specialname rtspecialname instance void .ctor() cil managed" /><MemberType>Constructor</MemberType><AssemblyInfo><AssemblyVersion>1.0.5000.0</AssemblyVersion><AssemblyVersion>2.0.0.0</AssemblyVersion><AssemblyVersion>4.0.0.0</AssemblyVersion></AssemblyInfo><ReturnValue /><Parameters /><Docs><remarks><attribution license="cc4" from="Microsoft" modified="false" /><para>This constructor initializes the <see cref="P:System.Exception.Message" /> property of the new instance to a system-supplied message that describes the error, such as "Cannot cast from source type to destination type." This message takes into account the current system culture.</para><para>The following table shows the initial property values for an instance of <see cref="T:System.InvalidCastException" />.</para><list type="table"><listheader><item><term><para>Property </para></term><description><para>Value </para></description></item></listheader><item><term><para><see cref="P:System.Exception.InnerException" /></para></term><description><para>A null reference (Nothing in Visual Basic). </para></description></item><item><term><para><see cref="P:System.Exception.Message" /></para></term><description><para>The localized error message string. </para></description></item></list></remarks><summary><attribution license="cc4" from="Microsoft" modified="false" /><para>Initializes a new instance of the <see cref="T:System.InvalidCastException" /> class.</para></summary></Docs><Excluded>0</Excluded></Member><Member MemberName=".ctor"><MemberSignature Language="ILASM" Value="public rtspecialname specialname instance void .ctor(string message)" /><MemberSignature Language="C#" Value="public InvalidCastException (string message);" /><MemberSignature Language="ILAsm" Value=".method public hidebysig specialname rtspecialname instance void .ctor(string message) cil managed" /><MemberType>Constructor</MemberType><AssemblyInfo><AssemblyVersion>1.0.5000.0</AssemblyVersion><AssemblyVersion>2.0.0.0</AssemblyVersion><AssemblyVersion>4.0.0.0</AssemblyVersion></AssemblyInfo><ReturnValue /><Parameters><Parameter Name="message" Type="System.String" /></Parameters><Docs><remarks><attribution license="cc4" from="Microsoft" modified="false" /><para>This constructor initializes the <see cref="P:System.Exception.Message" /> property of the new exception using the <paramref name="message" /> parameter. The content of <paramref name="message" /> is intended to be understood by humans. The caller of this constructor is required to ensure that this string has been localized for the current system culture.</para><para>The following table shows the initial property values for an instance of <see cref="T:System.InvalidCastException" />.</para><list type="table"><listheader><item><term><para>Property </para></term><description><para>Value </para></description></item></listheader><item><term><para><see cref="P:System.Exception.InnerException" /></para></term><description><para>A null reference (Nothing in Visual Basic). </para></description></item><item><term><para><see cref="P:System.Exception.Message" /></para></term><description><para>The error message string. </para></description></item></list></remarks><summary><attribution license="cc4" from="Microsoft" modified="false" /><para>Initializes a new instance of the <see cref="T:System.InvalidCastException" /> class with a specified error message.</para></summary><param name="message"><attribution license="cc4" from="Microsoft" modified="false" />The message that describes the error. </param></Docs><Excluded>0</Excluded></Member><Member MemberName=".ctor"><MemberSignature Language="C#" Value="protected InvalidCastException (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);" /><MemberSignature Language="ILAsm" Value=".method familyhidebysig specialname rtspecialname instance void .ctor(class System.Runtime.Serialization.SerializationInfo info, valuetype System.Runtime.Serialization.StreamingContext context) cil managed" /><MemberType>Constructor</MemberType><AssemblyInfo><AssemblyVersion>1.0.5000.0</AssemblyVersion><AssemblyVersion>2.0.0.0</AssemblyVersion><AssemblyVersion>4.0.0.0</AssemblyVersion></AssemblyInfo><Parameters><Parameter Name="info" Type="System.Runtime.Serialization.SerializationInfo" /><Parameter Name="context" Type="System.Runtime.Serialization.StreamingContext" /></Parameters><Docs><remarks><attribution license="cc4" from="Microsoft" modified="false" /><para>This constructor is called during deserialization to reconstitute the exception object transmitted over a stream. For more information, see <format type="text/html"><a href="832ac524-21bc-419a-a27b-ca8bfc45840f">XML and Soap Serialization</a></format>.</para></remarks><summary><attribution license="cc4" from="Microsoft" modified="false" /><para>Initializes a new instance of the <see cref="T:System.InvalidCastException" /> class with serialized data.</para></summary><param name="info"><attribution license="cc4" from="Microsoft" modified="false" />The object that holds the serialized object data. </param><param name="context"><attribution license="cc4" from="Microsoft" modified="false" />The contextual information about the source or destination. </param></Docs></Member><Member MemberName=".ctor"><MemberSignature Language="ILASM" Value="public rtspecialname specialname instance void .ctor(string message, class System.Exception innerException)" /><MemberSignature Language="C#" Value="public InvalidCastException (string message, Exception innerException);" /><MemberSignature Language="ILAsm" Value=".method public hidebysig specialname rtspecialname instance void .ctor(string message, class System.Exception innerException) cil managed" /><MemberType>Constructor</MemberType><AssemblyInfo><AssemblyVersion>1.0.5000.0</AssemblyVersion><AssemblyVersion>2.0.0.0</AssemblyVersion><AssemblyVersion>4.0.0.0</AssemblyVersion></AssemblyInfo><ReturnValue /><Parameters><Parameter Name="message" Type="System.String" /><Parameter Name="innerException" Type="System.Exception" /></Parameters><Docs><remarks><attribution license="cc4" from="Microsoft" modified="false" /><para>An exception that is thrown as a direct result of a previous exception should include a reference to the previous exception in the <see cref="P:System.Exception.InnerException" /> property. The <see cref="P:System.Exception.InnerException" /> property returns the same value that is passed into the constructor, or returns null if the <see cref="P:System.Exception.InnerException" /> property does not supply the inner exception value to the constructor.</para><para>The following table shows the initial property values for an instance of <see cref="T:System.InvalidCastException" />.</para><list type="table"><listheader><item><term><para>Property </para></term><description><para>Value </para></description></item></listheader><item><term><para><see cref="P:System.Exception.InnerException" /></para></term><description><para>The inner exception reference. </para></description></item><item><term><para><see cref="P:System.Exception.Message" /></para></term><description><para>The error message string. </para></description></item></list></remarks><summary><attribution license="cc4" from="Microsoft" modified="false" /><para>Initializes a new instance of the <see cref="T:System.InvalidCastException" /> class with a specified error message and a reference to the inner exception that is the cause of this exception.</para></summary><param name="message"><attribution license="cc4" from="Microsoft" modified="false" />The error message that explains the reason for the exception. </param><param name="innerException"><attribution license="cc4" from="Microsoft" modified="false" />The exception that is the cause of the current exception. If the <paramref name="innerException" /> parameter is not null, the current exception is raised in a catch block that handles the inner exception. </param></Docs><Excluded>0</Excluded></Member><Member MemberName=".ctor"><MemberSignature Language="C#" Value="public InvalidCastException (string message, int errorCode);" /><MemberSignature Language="ILAsm" Value=".method public hidebysig specialname rtspecialname instance void .ctor(string message, int32 errorCode) cil managed" /><MemberType>Constructor</MemberType><AssemblyInfo><AssemblyVersion>2.0.0.0</AssemblyVersion><AssemblyVersion>4.0.0.0</AssemblyVersion></AssemblyInfo><Parameters><Parameter Name="message" Type="System.String" /><Parameter Name="errorCode" Type="System.Int32" /></Parameters><Docs><since version=".NET 2.0" /><remarks><attribution license="cc4" from="Microsoft" modified="false" /><para>This constructor initializes the <see cref="P:System.Exception.Message" /> property of the new exception using the <paramref name="message" /> parameter. The content of <paramref name="message" /> is intended to be understood by humans. The caller of this constructor is required to ensure that this string has been localized for the current system culture.</para><para>This constructor supplies an HRESULT value that is accessible to inheritors of the <see cref="T:System.InvalidCastException" /> class, via the protected <see cref="P:System.Exception.HResult" /> property of the <see cref="T:System.Exception" /> class.</para><para>The following table shows the initial property values for an instance of <see cref="T:System.InvalidCastException" />.</para><list type="table"><listheader><item><term><para>Property </para></term><description><para>Value </para></description></item></listheader><item><term><para><see cref="P:System.Exception.InnerException" /></para></term><description><para>A null reference (Nothing in Visual Basic). </para></description></item><item><term><para><see cref="P:System.Exception.Message" /></para></term><description><para>The error message string. </para></description></item></list></remarks><summary><attribution license="cc4" from="Microsoft" modified="false" /><para>Initializes a new instance of the <see cref="T:System.InvalidCastException" /> class with a specified message and error code.</para></summary><param name="message"><attribution license="cc4" from="Microsoft" modified="false" />The message that indicates the reason the exception occurred.</param><param name="errorCode"><attribution license="cc4" from="Microsoft" modified="false" />The error code (HRESULT) value associated with the exception.</param></Docs></Member></Members><TypeExcluded>0</TypeExcluded></Type>