Is there any difference in following two lines of code that compares the string values.
string str = "abc";
if(str.Equals("abc"))
and
if("abc".Equals(str))
in the first line I am calling the equals method on string variable to compare it with string literal. The second line is vice versa. Is it just the difference of coding style or there is a difference in the way these two statements are processed by the compiler.
Yes, the way the compiler processed the statements is different. The function equals for String in most languages follows the same guidlines. Here is a semicode:
Normally, the method will fisrt check that that IS a String, and that this and that have the same length.
You can see, as others pointed out, that if that is
nullthe method returns false. On the other hand, the method is part of of String so it cannot be called onnull. That is why in your exampleif str is null you will get aNullReferenceException.That being said, if you know both variables are non-null Strings of the same length, both statements will evaluate to the same in the same time.