CSE459_CSharp_07_Equality

CSE459_CSharp_07_Equality - Programming in C# Equality CSE...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Programming in C# Equality CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Equality l What does it mean for to variables to be equal: l Numeric types – easy l Strings – some caveats l Employee records - ?
Background image of page 2
Equality l Two basic types of equality: l Value equality l Two variables are equal if they have the same value (mean the same thing). l Referential equality l Two variables are equal if they refer to the same instance (pointer or storage equality). l Difference: l Are two 2008 Lamborghini Gallardo’s equal? l Theoretically, it depends on the context .
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Default Equality l As expected, all value types have value- based equality. double a = 1.2; double b = 1.2; bool areEqual = a == b; // true. l Strings also have value equality. string name = “Crawfis” ; string instructor = “Crawfis” ; bool areEqual = a == b; // true.
Background image of page 4
Default Equality l What is the default equality for classes? l In C++ there is none. l In C# there is. It is referential equality. class foo {…}; foo A = new foo(); foo B = new foo(); bool areEqual = A == B; // false . object C = A; // C refers to the same instance as A. bool areEqual = C == A; // true .
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Default Equality l Subtleties: int [] a = {0,1,2,3}; int [] b = {0,1,2,3}; int [] a = {0,1,2,3}; int [] b = a; int [] a = null ; int [] b = null ; a == b => false a == b => true a == b => true!
Background image of page 6
Default Equality l The default equality for structs is simply the pairwise comparison between each of its fields. l That is, two structs are equal if all of their fields are equal. l Value-based field are compared by value. l Reference fields are compared by reference (by default).
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Testing for Equality l There are actually five protocols or methods that you can use to test for equality. l They may provide different results!!! l Witness one says they are the same. l Witness two says they are different.
Background image of page 8
l Operators for equality (== and !=). l Recall that all operators are defined as static methods for a type. l If there are different types on each side of the conditional, the compiler decides which type to use. l
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 33

CSE459_CSharp_07_Equality - Programming in C# Equality CSE...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online