111,126
社区成员
发帖
与我相关
我的任务
分享
Dictionary<byte[], int> dic = new Dictionary<byte[], int>();
byte[] b = new byte[2];
b[0] = 12;
b[1] = 23;
dic.Add(b, 200);
byte[] b1 = new byte[8];
b1[0] = 12;
b1[1] = 23;
Response.Write(isExistsKey(dic, b1));
private bool isExistsKey(Dictionary<byte[], int> dic, byte[] b)
{
string _bStr, bStr;
foreach (byte[] _b in dic.Keys)
{
_bStr = Encoding.UTF8.GetString(_b);
bStr = Encoding.UTF8.GetString(b);
_bStr = _bStr.Contains("\0") ? _bStr.Substring(0, _bStr.IndexOf("\0")) : _bStr;
bStr = bStr.Contains("\0") ? bStr.Substring(0, bStr.IndexOf("\0")) : bStr;
if (_bStr == bStr)
{
return true;
}
}
return false;
}
Dictionary<List<byte>, int> dic = new Dictionary<List<byte>, int>();
List<byte> listByte = new List<byte>();
//假设字典dic里的一个键值对的索引是listByte,值是3
if(dic.Contains(new KeyValuePair<List<byte>,int>(listByte,3)))
{
Response.Write("存在");
}
else
{
Response.Write("不存在");
}
public static void Main()
{
Dictionary<ListBytes, int> dic = new Dictionary<ListBytes, int>();
ListBytes bytes = new ListBytes();
bytes.Add(12);
bytes.Add(23);
dic.Add(bytes, 233);
ListBytes bytes2 = new ListBytes();
bytes2.Add(12);
bytes2.Add(23);
int value = 0;
if (dic.TryGetValue(bytes2, out value))
Console.WriteLine(value);
}
public class ListBytes : List<byte>
{
public override int GetHashCode()
{
int hash = 0;
for (int i = 0; i < this.Count; i++)
hash += this[i].GetHashCode();
return hash;
}
public override bool Equals(object obj)
{
if (!(obj is List<byte>))
return false;
List<byte> tar = obj as List<byte>;
if (tar.Count != this.Count)
return false;
for (int i = 0; i < tar.Count; i++)
if (this[i] != tar[i])
return false;
return true;
}
}
Dictionary<(Of <(TKey, TValue>)>)..::.Contains 方法
Dictionary<(Of <(TKey, TValue>)>) 类 请参见 发送反馈意见
重载列表
名称 说明
Contains(KeyValuePair<(Of <(UTP, UTP>)>)) 通过使用默认的相等比较器确定序列是否包含指定的元素。 (由 Enumerable 定义。)
Contains(KeyValuePair<(Of <(UTP, UTP>)>), IEqualityComparer<(Of <(KeyValuePair<(Of <(UTP, UTP>)>)>)>)) 通过使用指定的 IEqualityComparer<(Of <(T>)>) 确定序列是否包含指定的元素。 (由 Enumerable 定义。)