Following is the code snippet from a console application -
class MyClass
{
public int GetDay(string data22)
{
int returnValue = 0;
if (string.Compare(data22,"THURSDAY") == 0) // true
{
returnValue = (int)DayOfWeek.Thursday;
}
if (data22 == "THURSDAY") //false
{
returnValue = (int)DayOfWeek.Thursday;
}
if (string.Equals(data22, "THURSDAY"))//false
{
returnValue = (int)DayOfWeek.Thursday;
}
return returnValue;
}
}
class Program
{
static void Main(string[] args)
{
string ExecutionDay = "THURSDAY";
MyClass obj1 = new MyClass();
int MyDays = obj1.GetDay(ExecutionDay);
}
}
Question is - Why does the first comparison (string.compare) work and the other two comparison methods does not work in THIS PARTICULAR CASE ?
There are invisible characters (particularly, a Left-to-Right mark (Thanks @MatthewWatson)) in your code. You can view them with any hex editor:
This is over-looked by
string.Compare
, while it isn't withstring.Equals
. You can see it in the docs: