Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>CLR allows you to cast an object to its type or any of it's base type. A cast to base type is already considered safe and is implicit.</strong></p> <p>for eg. </p> <p>Object s = new String();</p> <p><strong>But the CLR wants you to specify explicit cast when casting to a derived type</strong></p> <pre><code>String str = s; // Give error and needs you to explicitly cast it //To get it to work String str = (String)s; </code></pre> <p>Now here what happens it s is not converted to string, but checked whether is of type String or not. If it is found to be of type String the cast is success else an InvalidCastExcetion is thrown.</p> <p>Two more ways to case are using <strong>is</strong> and <strong>as</strong> operators</p> <p>is Operator : This returns true if the cast succeeds else false.</p> <p>e.g.</p> <pre><code>Object o = new Object(); Boolean b1 = (o is Object); // b1 is true Boolean b2 = (o is DateTime); // b2 is false </code></pre> <p>So before calling any method, normally you will write code like this</p> <pre><code>if(o is DateTime) // Check this by observing the object { DateTime d = (DateTime)o; // Again cast the object to obtain a reference // Perform operations } </code></pre> <p>This is a little expensive because CLR casts twice.</p> <p>To avoid this we have the <strong>as</strong> operator.</p> <p>as Operator : Returns the reference to the type of object checked else returns null.</p> <p>e.g. :</p> <pre><code>DateTime d = o as DateTime; // Check the object to verify the case and returns reference to the object itself or null if(d != null) { // Perform the operations } </code></pre> <p>So you can see, there is a slight performance boost when using as operator.</p> <p>That is all what the CLR has to offer when it comes to casting types.</p> <hr> <p>When it comes to your code :</p> <p>object str = "abc";</p> <p>str.ToString() will call the ToString method of the System.object class which is a virtual method. When calling a virtual method, CLR will actually check the type of the object pointed by the caller.</p> <p>Here str is actually pointing to a string object. So compiler will generate code to call the ToString method of the String class which will print the "abc" as output. <strong>This concept is polymorphism in which when calling a virtual method of any type, the actual object type is first obtained by the CLR and then appropiate method is called on the correct type of the object as String type in your case.</strong></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. This table or related slice is empty.
    1. 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