Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The first type of cast is called an "explicit cast" and the second cast is actually a conversion using the <code>as</code> operator, which is slightly different than a cast.</p> <p>The explicit cast <code>(type)objectInstance</code> will throw an <code>InvalidCastException</code> if the object is not of the specified type.</p> <pre><code>// throws an exception if myObject is not of type MyTypeObject. MyTypedObject mto = (MyTypedObject)myObject; </code></pre> <p>The <code>as</code> operator will not throw an exception if the object is not of the specified type. It will simply return <code>null</code>. If the object is of the specified type then the <code>as</code> operator will return a reference to the converted type. The typical pattern for using the <code>as</code> operator is:</p> <pre><code>// no exception thrown if myObject is not MyTypedObject MyTypedObject mto = myObject as MyTypedObject; if (mto != null) { // myObject was of type MyTypedObject, mto is a reference to the converted myObject } else { // myObject was of not type MyTypedObject, mto is null } </code></pre> <p>Take a look at the following MSDN references for more details about explicit casting and type conversion:</p> <ul> <li><a href="http://msdn.microsoft.com/en-us/library/ms173105.aspx" rel="noreferrer">Casting (C#)</a></li> <li><a href="http://msdn.microsoft.com/en-us/library/ms173105.aspx" rel="noreferrer">Casting and Type Conversions</a></li> <li><a href="http://msdn.microsoft.com/en-us/library/cc488006.aspx" rel="noreferrer">How to: Safely Cast by Using as and is Operators</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload