Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do these two comparisons have different results?

Tags:

c#

.net

equality

Why does this code return true:

new Byte() == new Byte()   // returns true 

but this code returns false:

new Byte[0] == new Byte[0] // returns false 
like image 972
Maxim Zhukov Avatar asked Jan 14 '14 19:01

Maxim Zhukov


People also ask

What is the purpose of a multiple comparison test?

Multiple comparisons tests (MCTs) are performed several times on the mean of experimental conditions. When the null hypothesis is rejected in a validation, MCTs are performed when certain experimental conditions have a statistically significant mean difference or there is a specific aspect between the group means.

How can you tell if two groups are statistically different?

The determination of whether there is a statistically significant difference between the two means is reported as a p-value. Typically, if the p-value is below a certain level (usually 0.05), the conclusion is that there is a difference between the two group means.

Why are multiple comparisons a challenge in statistics?

A drawback of this approach is that it overstates the evidence that some of the alternative hypotheses are true when the test statistics are positively correlated, which commonly occurs in practice..

What is the purpose of using a multiple comparisons test after an ANOVA?

For k groups, ANOVA can be used to look for a difference across k group means as a whole. If there is a statistically significant difference across k means then a multiple comparison method can be used to look for specific differences between pairs of groups.


2 Answers

Because new Byte() creates value type, which are compared by value (by default it will return byte with value 0). And new Byte[0] creates array, which is a reference type and compared by reference (and these two instances of array will have different references).

See Value Types and Reference Types article for details.

like image 159
Sergey Berezovskiy Avatar answered Sep 24 '22 15:09

Sergey Berezovskiy


Bytes are value types in .NET, meaning that the == operator returns true if and only if the two bytes have the same value. This is also known as value equality.

But arrays are reference types in .NET, meaning the == operator returns true if and only if they refer to the same array instance in memory. This is also known as reference equality or identity.

Note that the == operator can be overloaded for both reference and value types. System.String, for example, is a reference type, but the == operator for strings compares each character in the array in sequence. See Guidelines for Overloading Equals() and Operator == (C# Programming Guide).

If you want to test whether the arrays contain exactly the same values (in order) you should consider using Enumerable.SequenceEqual instead of ==.

like image 44
p.s.w.g Avatar answered Sep 24 '22 15:09

p.s.w.g