Basic question…… RRS feed

  • Question

  • http://msdn.microsoft.com/en-us/library/362314fe(v=vs.80).aspx


    In the above link…..


    1.  Console.WriteLine((object)a == (object)b);

    Output of (object)b is “hello”. Why “(object)a == (object)b” is not matching? Why compiler is picking the reference of first “b” initialization.


    2.  Similarly

    string b = "h";
    b += "ello";


    The document says  the variable b continues to hold "h". When did this “h”, the String object became a variable? How to access the variable which holds the value “h”?


    Missing something here to understand.

    Please advice.


    Friday, December 9, 2011 7:59 PM


  • I'd give you the answer,  but the link you provided has all the answeres


    1:  This displays "True" and then "False" because the content of the strings are equivalent, but a and b do not refer to the same string instance.

    When you cast the strings to the object type, they handle "==" operator differently


    2.  After reading the link you provided,  the variable that holds the value of "h" is no longer referenced, and is deleted by the garbage collector, unless you alias it to somewhere else.



    Friday, December 9, 2011 8:07 PM