[. Net] compares two byte arrays.

Source: Internet
Author: User

In the morning, a beginner asked: how do I compare the two byte arrays to see if each byte is equal?

You are not allowed to laugh. I have always opposed to ridicule beginners. You can criticize them when they do not study hard, but not ridicule them. When you start learning programming, what is the background?

Well, this beginner friend made the following attempts:

            [] buffer1 = { , , [] buffer2 = { , , == buffer2);

Can this method be used? As a result, the friend almost fainted. Buffer1 = buffer2 compares the two byte [] instances and does not compare the elements in them.

After learning the lesson, this friend modified the Code as follows:

            [] buffer1 = { , , [] buffer2 = { , ,  isEq =  (buffer1.Length !== =  ( i = ; i < buffer1.Length; i++ (buffer1[i] !== 

This time it worked. Come and applaud the achievement of this friend.

At this point, it may be okay, but this friend is very persistent. He continues to ask: "Is there a simpler way ?"

I didn't answer, so he started his mind and soon wrote a piece of code:

         IsEqual( [] src,  isEq =  (src.Length !== =  ( i = ; i < src.Length; i++ (src[i] !== 

He wrote the code just now to the extension method. The extension object is of the byte [] type. In the future, you don't have to write so much code every time in this case. You can directly call the extension method.

            Console.WriteLine(buffer1.IsEqual(buffer2));

Good. It seems that this student is very savvy and understands how to use the extension method. However, the middleware is still not satisfied and is not simple enough.

Actually ,. the NET class library already has a ready-made method, which is also an extension method. To compare the elements in two sequences (such as arrays and lists), you only need to call this method, you do not need to write your own code for implementation. What is the solution? This method is named SequenceEqual.

Therefore, we can simply complete the code of the entire program.

            [] buffer1 = { , , [] buffer2 = { , , 

This dear friend, he is very excited! It turns out this way! Sun Walker is just as excited as he finds a good stick in the Dragon Palace.

Some may ask again, what if we compare the elements with the classes we write? I need my own comparison rules. The SequenceEqual method also has an overload, that is, an object that implements the IEqualityComparer <T> interface can be passed to the method parameter.

Because the EqualityComparer <T> class implements this interface, it can be derived directly from this class when I customize the comparator.

For example, we have defined a "handsome guy" class, which contains two attributes: Name and ID. For any two "handsome guy" objects, we think they are the same handsome guy as long as their IDs are equal.

       ID { ;   Name { ; 

Then, we define a comparator specifically used to determine whether two handsome guys are the same handsome guy.

Handsome guy: EqualityComparer 


Come on, start the test.

Handsome guy [] sgarr1 = handsome guy {ID =, Name = handsome guy {ID =, name = handsome guy {ID =, Name = handsome guy ~itycomparer comparer =

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.