Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Comparing byte arrays does not work
Message
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01489235
Message ID:
01489249
Views:
43
>Considder the code below:
>
>			byte[] one = new byte[] { 0, 1, 2, 3, 4 };
>			byte[] two = new byte[] { 0, 1, 2, 3, 4 };
>
>			if (one != two)
>			{
>				MessageBox.Show("DIFFERENT", "COMAPARE");
>			}
>			else
>			{
>				MessageBox.Show("SAME", "COMAPARE");
>			}
>
>			///////
>
>			bool different = false;
>			for (int i = 0; i < one.Length; i++)
>			{
>				if (one[i] != two[i])
>				{
>					different = true;
>					break;
>				}
>			}
>			if (different)
>			{
>				MessageBox.Show("DIFFERENT", "LOOP");
>			}
>			else
>			{
>				MessageBox.Show("SAME", "LOOP");
>			}
>
>
>Just checking for non-equality (!=) says the byte arrays are different, if I loop though all the elements I find that the byte arrays are the same. I am trying to come up with a good reason why it was implemented like this. Any help is appreciated.

You're confusing value equality with reference equality. Default comparison is reference.
byte[] copy = two;
if (copy == two)
{
//true;
}
>Wow it has been long since I have been on UT.

True....
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform