我有一个包含字符串作为键,而System.Object作为值的字典。
Dictionary<string, System.Object> test = new Dictionary<string, System.Object>();
我创建并添加值
Debug.Log("Key:" + (string)KEY.obj);
Debug.Log("VALUE:" + (string)VALUE.obj);
test.Add((string)KEY.obj, VALUE.obj);
if (test.ContainsKey("id")){
Debug.Log("contains!");
}
else{
Debug.Log("does not contain!");
}
调试控制台
KEY和VALUE是类
的对象private sealed class ResolveValue {
public readonly System.Object obj;
public readonly int end;
public ResolveValue(System.Object obj, int end){
this.obj = obj;
this.end = end;
}
}
KEY是&#34; id&#34;和VALUE是&#34; myData&#34;。
我花了很长时间才发现问题。 这怎么可能?
其他信息
所有字符串都是来自字节的转换器。 这是我的调试。
Debug.Log("--------");
string msg = "";
foreach(byte b in bytes){
msg += b + " ";
}
Debug.Log(msg);
Debug.Log("--------");
string message = System.Text.Encoding.UTF8.GetString(bytes);
Debug.Log(message);
ResolveValue response = new ResolveValue(message, end);
变量 end 是一个整数,与此问题无关。
答案 0 :(得分:1)
您的字节数组中有两个空字节,它们将转换为您的字符串。他们不会出现在字符串表示中,但它们是不同的字符串。
byte[] bytes= System.Text.Encoding.UTF8.GetBytes("id");
foreach (byte b in bytes)
{Console.WriteLine(b);}
产量
105
100
虽然
var b1 = new byte[]{105,100,0,0};
var b2 = new byte[]{105,100};
Console.WriteLine(System.Text.Encoding.UTF8.GetString(b1)==System.Text.Encoding.UTF8.GetString(b2));
收益率
False