A simple question related to strings in c#
-
Why the outputs are not the same? Char[] aChar = new Char[] { 'h', 'e', 'l', 'l', 'o' }; Char[] bChar = new Char[] { 'h', 'e', 'l', 'l', 'o' }; string a = new string(aChar); string b = new string(bChar); object c = a; object d = b; Console.WriteLine(c == d); String e = "hello"; String f = "hello"; object g = e; object h = f; Console.WriteLine(g == h); Regards, Diana.
-
Why the outputs are not the same? Char[] aChar = new Char[] { 'h', 'e', 'l', 'l', 'o' }; Char[] bChar = new Char[] { 'h', 'e', 'l', 'l', 'o' }; string a = new string(aChar); string b = new string(bChar); object c = a; object d = b; Console.WriteLine(c == d); String e = "hello"; String f = "hello"; object g = e; object h = f; Console.WriteLine(g == h); Regards, Diana.
because achar and bchar are two character arrays while e and f are two string variables thus they r not equal reply by dhamija.manoj@gmail.com manojdhamija@yahoo.co.in manoj
-
Why the outputs are not the same? Char[] aChar = new Char[] { 'h', 'e', 'l', 'l', 'o' }; Char[] bChar = new Char[] { 'h', 'e', 'l', 'l', 'o' }; string a = new string(aChar); string b = new string(bChar); object c = a; object d = b; Console.WriteLine(c == d); String e = "hello"; String f = "hello"; object g = e; object h = f; Console.WriteLine(g == h); Regards, Diana.
-
Why the outputs are not the same? Char[] aChar = new Char[] { 'h', 'e', 'l', 'l', 'o' }; Char[] bChar = new Char[] { 'h', 'e', 'l', 'l', 'o' }; string a = new string(aChar); string b = new string(bChar); object c = a; object d = b; Console.WriteLine(c == d); String e = "hello"; String f = "hello"; object g = e; object h = f; Console.WriteLine(g == h); Regards, Diana.
The compiler regognises that the e and f strings are identical, and uses the same value for them. As e and f references the same string, the objects g and h will also reference the same string. When you compare two object references, they will only be considered equal if they reference the same object. --- b { font-weight: normal; }
-
The compiler regognises that the e and f strings are identical, and uses the same value for them. As e and f references the same string, the objects g and h will also reference the same string. When you compare two object references, they will only be considered equal if they reference the same object. --- b { font-weight: normal; }
Hi You mean all the identical strings will have same reference? Thanks
-
Hi You mean all the identical strings will have same reference? Thanks