How to compare two byte arrays in C#

4 Answers

0 votes
using System;
using System.Linq;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            var arr1 = new byte[] { 1, 2, 3, 4, 5 };
            var arr2 = new byte[] { 1, 2, 3, 4, 5 };

            var b = arr1.SequenceEqual(arr2);

            Console.WriteLine(b);
        }
    }
}


/*
run:
 
True
 
*/

 



answered Apr 29, 2017 by avibootz
0 votes
using System;
using System.Linq;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            var arr1 = new byte[] { 1, 2, 3, 4, 5 };
            var arr2 = new byte[] { 1, 2, 3, 4, 7 };

            var b = arr1.SequenceEqual(arr2);

            Console.WriteLine(b);
        }
    }
}


/*
run:
 
False
 
*/

 



answered Apr 29, 2017 by avibootz
0 votes
using System;
using System.Runtime.InteropServices;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        [DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
        static extern int memcmp(byte[] arr1, byte[] arr2, long count);

        static void Main(string[] args)
        {
            var arr1 = new byte[] { 1, 2, 3, 4, 5 };
            var arr2 = new byte[] { 1, 2, 3, 4, 5 };

            bool b = arr1.Length == arr2.Length && memcmp(arr1, arr2, arr2.Length) == 0;

            Console.WriteLine(b);
        }
    }
}


/*
run:
 
True
 
*/

 



answered Apr 29, 2017 by avibootz
0 votes
using System;
using System.Collections;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            var arr1 = new byte[] { 1, 2, 3, 4, 5 };
            var arr2 = new byte[] { 1, 2, 3, 4, 5 };

            bool b = StructuralComparisons.StructuralEqualityComparer.Equals(arr1, arr2);

            Console.WriteLine(b);
        }
    }
}


/*
run:
 
True
 
*/

 



answered Apr 29, 2017 by avibootz

Related questions

1 answer 151 views
151 views asked Jun 5, 2023 by avibootz
1 answer 191 views
191 views asked Sep 23, 2016 by avibootz
1 answer 66 views
66 views asked Jul 12, 2025 by avibootz
2 answers 172 views
2 answers 144 views
3 answers 243 views
243 views asked Jan 25, 2022 by avibootz
...