Problem with Dictionary Value type returned as an argument to a callback

This top code snippet works if the Dictionary is accessed directly and it’s not returned in a call back.

    properties = new Dictionary<string, object>();
    properties.Add("Blue", "Dan");
    properties.Add("Red", "Joe");
  
    foreach(KeyValuePair<string, object> pair in properties)
    {
       	if(pair.Value == "Dan" )
	      {
		        // executed when pair.Value == "Dan"
		        Debug.Log(pair.Value + "'s color is " + pair.Key +"\n");
	      }
        Debug.Log("KEY:" + pair.Key + " =  VALUE:" + pair.Value + "\n");
    }

OUTPUT:
Dan’s color is Blue
KEY:Blue = VALUE:“Dan”
KEY:Red = VALUE:“Joe”


The above Dictionatry is made of the same type as the properties argument returned to
the onUserChangeRoomProperty() callBack below. But the same code as above doesn’t work
in the callback. It’s as if the Dictionatry value <type: Object> can’t be resolved or
converted to an accurate string that works in a comparison.

public void onUserChangeRoomProperty(RoomData roomData, string sender, Dictionary<String, Object> properties)
    {
        foreach (KeyValuePair<String, Object> pair in properties)
        {
        	  // NOTE sender == "Dan" in this case
        	
		        // if( sender == "Dan" )						// This works as expected. 
        		// if( sender == pair.Value.ToString() )		// This doesn't work.
	        	if( sender == pair.Value )						// This doesn't work. 			
	        	{
	          		// Never executed
	          		Debug.Log(pair.Value + "'s color is " + pair.Key +"\n");
	        	}
			
        	Debug.Log("KEY:" + pair.Key + " =  VALUE:" + pair.Value + "\n");
        }
    }

OUTPUT:
KEY:Blue = VALUE:“Dan”
KEY:Red = VALUE:“Joe”

Try this code:

 object o = "Dan";
print(o.GetType());

The result will be “System.String”.
By putting your string literal into an object variable, it is cast to String (with an uppercase S), which is a reference type.

You can test it with this code:

void Start()
{
    object o = "Dan";
    object o2 = "Da"+GetN();
    print(o == o2);
}

string GetN()
{
    return "n";
}

It will give you “False”.
Every Java Developer knows: Reference Type Strings are a bitch. There aren’t many ways to describe it better.

So, by casting your string to a String, you doomed yourself to use Java habits and compare Strings with Equal() instead of ==.