Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>string s = (string)o; // 1 </code></pre> <p>Throws <a href="https://msdn.microsoft.com/en-us/library/system.invalidcastexception" rel="noreferrer">InvalidCastException</a> if <code>o</code> is not a <code>string</code>. Otherwise, assigns <code>o</code> to <code>s</code>, even if <code>o</code> is <code>null</code>.</p> <pre><code>string s = o as string; // 2 </code></pre> <p>Assigns <code>null</code> to <code>s</code> if <code>o</code> is not a <code>string</code> or if <code>o</code> is <code>null</code>. For this reason, you cannot use it with value types (the operator could never return <code>null</code> in that case). Otherwise, assigns <code>o</code> to <code>s</code>.</p> <pre><code>string s = o.ToString(); // 3 </code></pre> <p>Causes a <a href="https://msdn.microsoft.com/en-us/library/system.nullreferenceexception" rel="noreferrer">NullReferenceException</a> if <code>o</code> is <code>null</code>. Assigns whatever <code>o.ToString()</code> returns to <code>s</code>, no matter what type <code>o</code> is.</p> <hr> <p>Use 1 for most conversions - it's simple and straightforward. I tend to almost never use 2 since if something is not the right type, I usually expect an exception to occur. I have only seen a need for this return-null type of functionality with badly designed libraries which use error codes (e.g. return null = error, instead of using exceptions).</p> <p>3 is not a cast and is just a method invocation. Use it for when you need the string representation of a non-string object.</p>
    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.
 

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