Answer: </P><P align=justify>In the past, you had to call .ToString() on the strings when using the == or != operators to compare the strings' values. That will still work, but the C# compiler now automatically compares the values instead of the references when the == or != operators are used on string types. If you actually do want to compare references, it can be done as follows: if ((object) str1 == (object) str2) { ... }</P><P align=justify>Here's an example showing how string compares work: using System;</P><P align=justify>public class StringTest</P><P align=justify>{</P><P align=justify>public static void Main(string[] args)</P><P align=justify>{</P><P align=justify>Object nullObj = null;</P><P align=justify>Object realObj = new StringTest();</P><P align=justify>int i = 10;</P><P align=justify></P><P align=justify>Console.WriteLine("Null Object is [" + nullObj + "]n" +</P><P align=justify>"Real Object is [" + realObj + "]n" +</P><P align=justify>"i is [" + i + "]n"); </P><P align=justify></P><P align=justify>// Show string equality operators</P><P align=justify>string str1 = "foo";</P><P align=justify>string str2 = "bar";</P><P align=justify>string str3 = "bar";</P><P align=justify></P><P align=justify>Console.WriteLine("{0} == {1} ? {2}", str1, str2, str1 == str2 );</P><P align=justify>Console.WriteLine("{0} == {1} ? {2}", str2, str3, str2 == str3 );</P><P align=justify>}</P><P align=justify>}</P><P align=justify></P><P align=justify>Output: Null Object is []</P><P align=justify>Real Object is [StringTest]</P><P align=justify>i is [10]</P><P align=justify></P><P align=justify>foo == bar ? False</P><P align=justify>bar == bar ? True
Tags:Interview Questions, Microsoft, C#,
Please submit your Interview Answer for the above Question via replying to this post. Thank you for visiting to our blog, Have a Good Day!.
Sponsor Advertisement
School Management System Software
This is One of the best School management system software in the world. School Software offers complete professional institute management system administration, library, accounting, inventory, etc for all the school activities management...
No comments:
Post a Comment