Equality Array(Data Structure tips in C#)

Hello guys, today I want to focus on Array, actually, I hope you know about array and in this article, I try to a bit deeper into it. so let’s go…
I start with a sample:

define 2 DateTime array
I have defined exactly 2 DateTime arrays like each othe…


This content originally appeared on DEV Community and was authored by Shayan

Hello guys, today I want to focus on Array, actually, I hope you know about array and in this article, I try to a bit deeper into it. so let's go…
I start with a sample:

Image description

define 2 DateTime array
I have defined exactly 2 DateTime arrays like each other, just the names are different.
What do you think? are they equal to each other?

Image description

As you see, when using the ‘==’ operator for 2 arrays, however, each item is exactly like the other, the result is false!
I hope you remember Value-type and Reference-type.

Image description

When I define a value-Type like DateTime, each variable is put in memory like the above picture so “mydate!=yourdate”.Now let’s look at how Reference-Types are stored

Image description

Reference-Type means the variable doesn't actually contain the string ”Shayan” and stores the address of memory where store the variable(xx).
However DataTime is Value-Type,DataType[] is Referencetype because ,Array is Reference-Type

Image description

Therefore when defining 2 Arrays with the same values, they are located at different addresses, and bankHols1==bankHols2 is false (why???)
Because the “==” operator for ReferenceType check Addresses and addresses are different.
this rule is ok for all Reference-Type collections in c#, But be careful about “String”.
The string is reference type but Microsoft overrides the”==” operator for it so when using the “==” operator for 2 string that is the same as each other the result is true.

Image description

Now the question is: how can compare just the value of 2 arrays?
Microsoft introduces the SequenceEqual() extension method (in the LinQ)

Image description

But be careful when using SequenceEqual(), it is too expensive because comparing each pair of elements if it is a bit large could be a lot of operations.


This content originally appeared on DEV Community and was authored by Shayan


Print Share Comment Cite Upload Translate Updates
APA

Shayan | Sciencx (2022-04-20T02:33:33+00:00) Equality Array(Data Structure tips in C#). Retrieved from https://www.scien.cx/2022/04/20/equality-arraydata-structure-tips-in-c/

MLA
" » Equality Array(Data Structure tips in C#)." Shayan | Sciencx - Wednesday April 20, 2022, https://www.scien.cx/2022/04/20/equality-arraydata-structure-tips-in-c/
HARVARD
Shayan | Sciencx Wednesday April 20, 2022 » Equality Array(Data Structure tips in C#)., viewed ,<https://www.scien.cx/2022/04/20/equality-arraydata-structure-tips-in-c/>
VANCOUVER
Shayan | Sciencx - » Equality Array(Data Structure tips in C#). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/04/20/equality-arraydata-structure-tips-in-c/
CHICAGO
" » Equality Array(Data Structure tips in C#)." Shayan | Sciencx - Accessed . https://www.scien.cx/2022/04/20/equality-arraydata-structure-tips-in-c/
IEEE
" » Equality Array(Data Structure tips in C#)." Shayan | Sciencx [Online]. Available: https://www.scien.cx/2022/04/20/equality-arraydata-structure-tips-in-c/. [Accessed: ]
rf:citation
» Equality Array(Data Structure tips in C#) | Shayan | Sciencx | https://www.scien.cx/2022/04/20/equality-arraydata-structure-tips-in-c/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.