Reputation: 309
I have byte buffer:
byte[] buffer = new byte[3];
List<byte[]> list;
Now I am adding:
while ((count = reader.Read(buffer, 0, buffer.Length)) != 0)
{
bool contains = l.Contains<byte[]>(buffer); //This is not working and checking only reference
if (!contains)
{
l.Add(new byte[] buffer[0],buffer[1],buffer[2]});
}
}
How to check if list contains byte array wchich has the same values as buffer?
Upvotes: 3
Views: 5043
Reputation: 2068
public static bool ContainsSequence(byte[] toSearch, byte[] toFind) {
for (var i = 0; i + toFind.Length < toSearch.Length; i++) {
var allSame = true;
for (var j = 0; j < toFind.Length; j++) {
if (toSearch[i + j] != toFind[j]) {
allSame = false;
break;
}
}
if (allSame) {
return true;
}
}
return false;
}
Upvotes: 0
Reputation: 39085
Your current version is not working because it does a check based on reference equality.
You want to find out if any list elements contain the same sequence of bytes:
bool contains = list.Any(x => x.SequenceEqual(buffer));
Upvotes: 8