← Back
IAmTimCorey September 28, 2026 30m

Understanding Equality in C# - Equals(), ReferenceEquals(), and ==

Not yet indexed — Search & Ask will be available once this episode finishes processing.

Read full transcript 24 segments
  1. There are two common ways to compare There are two common ways to compare items in C. You can use the double items in C. You can use the double items in C. You can use the double equals or the equals method, but what's equals or the equals method, but what's equals or the equals method, but what's the difference? In this video, we're the difference? In this video, we're the difference? In this video, we're going to look at both of these plus the going to look at both of these plus the going to look at both of these plus the reference equals method to figure out reference equals method to figure out reference equals method to figure out how each works and then why we have the how each works and then why we have the how each works and then why we have the different ones. We'll also look at why different ones. We'll also look at why different ones. We'll also look at why the double equals can be dangerous. Now, the double equals can be dangerous. Now, the double equals can be dangerous. Now, if you don't know me, my name is Tim if you don't know me, my name is Tim if you don't know me, my name is Tim Corey and I help people become better Corey and I help people become better Corey and I help people become better developers, especially around C andnet developers, especially around C andnet developers, especially around C andnet topics. I have over a thousand videos topics. I have over a thousand videos topics. I have over a thousand videos here on this channel to help you in your here on this channel to help you in your here on this channel to help you in your journey. Now, for those of you looking journey. Now, for those of you looking journey. Now, for those of you looking for specific topics, I offer dozens of for specific topics, I offer dozens of for specific topics, I offer dozens of training courses at devforge.com. training courses at devforge.com. training courses at devforge.com. There you'll find full master courses on There you'll find full master courses on There you'll find full master courses on Unity game development, C#, and web Unity game development, C#, and web Unity game development, C#, and web development along with individual development along with individual development along with individual courses covering Git, Azure, SQL, AI, courses covering Git, Azure, SQL, AI, courses covering Git, Azure, SQL, AI, and much more. The proceeds of those and much more. The proceeds of those and much more. The proceeds of those course sales are what funds the free course sales are what funds the free course sales are what funds the free content like this. You'll also find even content like this. You'll also find even content like this. You'll also find even more free content on devforge.com. So more free content on devforge.com. So more free content on devforge.com. So definitely check it out. Okay, let's definitely check it out. Okay, let's definitely check it out. Okay, let's jump into our code. I've already started jump into our code. I've already started jump into our code. I've already started the code just so we can get some the code just so we can get some the code just so we can get some boilerplate out of the way. This is a boilerplate out of the way. This is a boilerplate out of the way. This is a console application and this is really console application and this is really console application and this is really an extension of a couple of videos I've an extension of a couple of videos I've an extension of a couple of videos I've done recently. One on uh reference types done recently. One on uh reference types done recently. One on uh reference types versus value types and another on the versus value types and another on the versus value types and another on the garbage collector. So, if you haven't garbage collector. So, if you haven't garbage collector. So, if you haven't watched those videos, I would encourage watched those videos, I would encourage watched those videos, I would encourage you to check them out because they're you to check them out because they're you to check them out because they're kind of foundational for understanding kind of foundational for understanding kind of foundational for understanding why these different things work the way why these different things work the way why these different things work the way they work. So, what we're going to do is they work. So, what we're going to do is they work. So, what we're going to do is start off really simple. By the way, if

  2. start off really simple. By the way, if start off really simple. By the way, if you want the source code, there's a link you want the source code, there's a link you want the source code, there's a link down the description to get that. We're down the description to get that. We're down the description to get that. We're going to say int count one equals going to say int count one equals going to say int count one equals let's go with five. let's go with five. let's go with five. And count two, I forgot to say count And count two, I forgot to say count And count two, I forgot to say count one. uh count two equals five as well. one. uh count two equals five as well. one. uh count two equals five as well. So we're got to be naming these So we're got to be naming these So we're got to be naming these variables really cleanly u but also variables really cleanly u but also variables really cleanly u but also feels generically because that's the way feels generically because that's the way feels generically because that's the way it is. We're going to say console right it is. We're going to say console right it is. We're going to say console right line. We'll do our string interpolation. line. We'll do our string interpolation. line. We'll do our string interpolation. We'll say count values are equal uh We'll say count values are equal uh We'll say count values are equal uh using equal equal equal using equal equal equal using equal equal equal and we're going to say count one equals and we're going to say count one equals and we're going to say count one equals count count count two. two. two. So that'll print off true or false, So that'll print off true or false, So that'll print off true or false, right? Are these two equal? Now we right? Are these two equal? Now we right? Are these two equal? Now we expect that is true and we're gonna see expect that is true and we're gonna see expect that is true and we're gonna see that. But we want to make sure that we that. But we want to make sure that we that. But we want to make sure that we start with a good baseline. So we're start with a good baseline. So we're start with a good baseline. So we're also going to use the uh equals.

  3. So what this would be is count one dot So what this would be is count one dot equals count 2. If you ever seen that equals count 2. If you ever seen that equals count 2. If you ever seen that comparison as well. So this is two comparison as well. So this is two comparison as well. So this is two different ways of doing equality checks. different ways of doing equality checks. different ways of doing equality checks. This first one's called an operator. So This first one's called an operator. So This first one's called an operator. So it's an operator. The second one is it's an operator. The second one is it's an operator. The second one is called a method. So we have operator way called a method. So we have operator way called a method. So we have operator way of doing it and a method way of doing of doing it and a method way of doing of doing it and a method way of doing it. And they seem to come out the same it. And they seem to come out the same it. And they seem to come out the same way because if we run this right now, way because if we run this right now, way because if we run this right now, let's wait for it to actually launch, let's wait for it to actually launch, let's wait for it to actually launch, but we'll see that true for both of but we'll see that true for both of but we'll see that true for both of those, which is what we would expect. those, which is what we would expect. those, which is what we would expect. Now, there is a third type. I'm going to Now, there is a third type. I'm going to Now, there is a third type. I'm going to use it right now and violate everything. use it right now and violate everything. use it right now and violate everything. Okay? So, just so you know, don't do Okay? So, just so you know, don't do Okay? So, just so you know, don't do this. I'm going to put right here, uh, this. I'm going to put right here, uh, this. I'm going to put right here, uh, don't do this. And that is to use reference do this. And that is to use reference equals. equals. equals. Okay. Uh and actually it's reference Okay. Uh and actually it's reference Okay. Uh and actually it's reference equals. Let me pass in uh count one and equals. Let me pass in uh count one and equals. Let me pass in uh count one and count two like so. Okay. Now it even gives you a like so. Okay. Now it even gives you a little warning here um that says hey uh little warning here um that says hey uh little warning here um that says hey uh don't pass an int for reference equals.

  4. don't pass an int for reference equals. don't pass an int for reference equals. Here's the reason why. Reference equal Here's the reason why. Reference equal Here's the reason why. Reference equal what it's going to do is find the what it's going to do is find the what it's going to do is find the reference to your object. But remember reference to your object. But remember reference to your object. But remember back to um reference versus value types. back to um reference versus value types. back to um reference versus value types. So integer is a value type, not a So integer is a value type, not a So integer is a value type, not a reference type. So how would you do a reference type. So how would you do a reference type. So how would you do a reference comparison between two value reference comparison between two value reference comparison between two value types? Well, what it's going to do is types? Well, what it's going to do is types? Well, what it's going to do is what's called boxing. If you've ever what's called boxing. If you've ever what's called boxing. If you've ever heard of boxing and you're not sure what heard of boxing and you're not sure what heard of boxing and you're not sure what it is, essentially what it does is it it is, essentially what it does is it it is, essentially what it does is it creates a reference object that is like creates a reference object that is like creates a reference object that is like a little box and then it puts the value a little box and then it puts the value a little box and then it puts the value type inside that box. So, it's going to type inside that box. So, it's going to type inside that box. So, it's going to create for us behind the scenes for this create for us behind the scenes for this create for us behind the scenes for this this call right here. It's going to this call right here. It's going to this call right here. It's going to create a box to put count one in and a create a box to put count one in and a create a box to put count one in and a box to put count two in. Three box to put count two in. Three box to put count two in. Three references. These will not be equal references. These will not be equal references. These will not be equal because it has two different boxes. because it has two different boxes. because it has two different boxes. Remember, a reference points to the Remember, a reference points to the Remember, a reference points to the location in memory. And so, they're two location in memory. And so, they're two location in memory. And so, they're two different locations because they're two different locations because they're two different locations because they're two different boxes, not the same box for different boxes, not the same box for different boxes, not the same box for two values. So don't do this for value two values. So don't do this for value two values. So don't do this for value types, but I wanted to put it in here to types, but I wanted to put it in here to types, but I wanted to put it in here to show you what of this is. Okay. So we're show you what of this is. Okay. So we're show you what of this is. Okay. So we're going to put a blank line here just so going to put a blank line here just so going to put a blank line here just so we have a little bit of separation.

  5. we have a little bit of separation. we have a little bit of separation. We're going to keep these going. I'm We're going to keep these going. I'm We're going to keep these going. I'm going to do something similar where I'm going to do something similar where I'm going to do something similar where I'm going to say uh person model P1. By the going to say uh person model P1. By the going to say uh person model P1. By the way, I've got a record down here called way, I've got a record down here called way, I've got a record down here called person record person model down here. person record person model down here. person record person model down here. It's a class. Okay. So, P1 It's a class. Okay. So, P1 It's a class. Okay. So, P1 equals new. These are very simple. Uh, equals new. These are very simple. Uh, equals new. These are very simple. Uh, first name equals Tim. first name equals Tim. first name equals Tim. Okay. And we're going to duplicate this. Okay. And we're going to duplicate this. Okay. And we're going to duplicate this. Uh, control D by the way duplicates in Uh, control D by the way duplicates in Uh, control D by the way duplicates in Visual Studio in case you didn't know Visual Studio in case you didn't know Visual Studio in case you didn't know that. Um, P two in the same value. Now, that. Um, P two in the same value. Now, that. Um, P two in the same value. Now, remember back to our reference type remember back to our reference type remember back to our reference type versus value type. The class is a versus value type. The class is a versus value type. The class is a reference type. An instance of a class reference type. An instance of a class reference type. An instance of a class is a reference type, which means it is a reference type, which means it is a reference type, which means it refers to a place in lo in memory. So refers to a place in lo in memory. So refers to a place in lo in memory. So therefore, even though they have the therefore, even though they have the therefore, even though they have the same values inside of them, they're two same values inside of them, they're two same values inside of them, they're two different objects, just like we when we different objects, just like we when we different objects, just like we when we boxed uh count one and count two. So boxed uh count one and count two. So boxed uh count one and count two. So let's see if these two what these two let's see if these two what these two let's see if these two what these two look like in it when it comes to the look like in it when it comes to the look like in it when it comes to the different types of equality. So we're different types of equality. So we're different types of equality. So we're going to say P1 going to say P1 going to say P1 uh P1 and P1 and then P2 P2 P2 P2 um and uh P1 and P1 and then P2 P2 P2 P2 um and uh P1 and P1 and then P2 P2 P2 P2 um and yes you can do this for for reference yes you can do this for for reference yes you can do this for for reference equals because of the fact they are equals because of the fact they are equals because of the fact they are reference types. Oh my way uh I should reference types. Oh my way uh I should reference types. Oh my way uh I should change this to reference equals.

  6. change this to reference equals. change this to reference equals. So um and then we're going to say uh So um and then we're going to say uh So um and then we're going to say uh let's go with uh person model let's go with uh person model let's go with uh person model values are equal or not. Okay. And if values are equal or not. Okay. And if values are equal or not. Okay. And if you run this now we'll see that person you run this now we'll see that person you run this now we'll see that person model values are not equal using double model values are not equal using double model values are not equal using double equals or equals or the reference type equals or equals or the reference type equals or equals or the reference type equals. Okay. So that should hopefully equals. Okay. So that should hopefully equals. Okay. So that should hopefully make sense and you should hopefully make sense and you should hopefully make sense and you should hopefully understand that. That's a a pretty understand that. That's a a pretty understand that. That's a a pretty common understanding. But we're going to common understanding. But we're going to common understanding. But we're going to go off the rails in this video and find go off the rails in this video and find go off the rails in this video and find all the edge cases or at least a lot of all the edge cases or at least a lot of all the edge cases or at least a lot of them. So, let's let's do that. Um, let's them. So, let's let's do that. Um, let's them. So, let's let's do that. Um, let's create an edge case here. I'm going to create an edge case here. I'm going to create an edge case here. I'm going to copy actually all of this and I'm going copy actually all of this and I'm going copy actually all of this and I'm going to paste it again. We're create P3, but to paste it again. We're create P3, but to paste it again. We're create P3, but P3 is going to be P3 is going to be P3 is going to be P1. P1. P1. So, we're going to put P1 into P3, So, we're going to put P1 into P3, So, we're going to put P1 into P3, right? which hopefully you know what's right? which hopefully you know what's right? which hopefully you know what's going to happen here. But I want to make going to happen here. But I want to make going to happen here. But I want to make sure we are clear on on what happens.

  7. sure we are clear on on what happens. sure we are clear on on what happens. Okay. So this last section down here we Okay. So this last section down here we Okay. So this last section down here we got true true and true. Why? Because of got true true and true. Why? Because of got true true and true. Why? Because of the fact that this this P3 took the same the fact that this this P3 took the same the fact that this this P3 took the same reference as P1. reference as P1. reference as P1. So what is the comparison right now? So what is the comparison right now? So what is the comparison right now? Let's set up here. This comparison is Let's set up here. This comparison is Let's set up here. This comparison is comparing the content or the value. Um, comparing the content or the value. Um, comparing the content or the value. Um, this comparison here is comparing the this comparison here is comparing the this comparison here is comparing the content or the value. But this content or the value. But this content or the value. But this comparison down here is comparing the comparison down here is comparing the comparison down here is comparing the reference. reference. reference. Okay? Not the value, the reference. Okay? Not the value, the reference. Okay? Not the value, the reference. Because what's actually stored in P1 and Because what's actually stored in P1 and Because what's actually stored in P1 and P2 and in P3 is going to be just the P2 and in P3 is going to be just the P2 and in P3 is going to be just the pointer to the location in memory. That pointer to the location in memory. That pointer to the location in memory. That means that the equals is doing the same means that the equals is doing the same means that the equals is doing the same thing and of course the reference equals thing and of course the reference equals thing and of course the reference equals is doing the same thing as well. So is doing the same thing as well. So is doing the same thing as well. So these are comparing the references which these are comparing the references which these are comparing the references which means they've got some comparisons here means they've got some comparisons here means they've got some comparisons here that a bit different with equals and that a bit different with equals and that a bit different with equals and equals double equals and equals u equals double equals and equals u equals double equals and equals u they're comparing the content whereas they're comparing the content whereas they're comparing the content whereas reference equals is comparing the reference equals is comparing the reference equals is comparing the reference location. Okay. Um and we can reference location. Okay. Um and we can reference location. Okay. Um and we can even say that's reference even say that's reference even say that's reference for value for value for value types. How about that? Be a little more types. How about that? Be a little more types. How about that? Be a little more clear.

  8. clear. clear. Okay, so so far hopefully you're on the Okay, so so far hopefully you're on the Okay, so so far hopefully you're on the same page. Hopefully you're like, "Yep, same page. Hopefully you're like, "Yep, same page. Hopefully you're like, "Yep, Tim, this is this is basic stuff." Well, Tim, this is this is basic stuff." Well, Tim, this is this is basic stuff." Well, let's take this off the rails then. So, let's take this off the rails then. So, let's take this off the rails then. So, I'm going to um I'm just going to copy I'm going to um I'm just going to copy I'm going to um I'm just going to copy just the uh just this so we have the just the uh just this so we have the just the uh just this so we have the have to watch me keep typing, but I'm have to watch me keep typing, but I'm have to watch me keep typing, but I'm going to say string. You're probably going to say string. You're probably going to say string. You're probably going uh oh, name one equals Tim. going uh oh, name one equals Tim. going uh oh, name one equals Tim. Remember, what type is string? Remember, what type is string? Remember, what type is string? It's a reference type, not a value type. It's a reference type, not a value type. It's a reference type, not a value type. Okay, it's a reference type. And if you Okay, it's a reference type. And if you Okay, it's a reference type. And if you didn't know that, go back and watch my didn't know that, go back and watch my didn't know that, go back and watch my videos earlier because um we talk about videos earlier because um we talk about videos earlier because um we talk about string and how it's a little weird string and how it's a little weird string and how it's a little weird because it is a reference type, but it because it is a reference type, but it because it is a reference type, but it sometimes acts like a value type. So sometimes acts like a value type. So sometimes acts like a value type. So name and then here we're compare for equality and then here we're compare for equality P1, P1, P1, I'm sorry, name one I'm sorry, name one I'm sorry, name one and then name two.

  9. Okay. Now, do you think that our Okay. Now, do you think that our reference, reference, and reference line reference, reference, and reference line reference, reference, and reference line up correctly? I want you to pause and up correctly? I want you to pause and up correctly? I want you to pause and think of this for a minute. What's it think of this for a minute. What's it think of this for a minute. What's it going to say for equality? Because you going to say for equality? Because you going to say for equality? Because you you know this, you've seen this. But you know this, you've seen this. But you know this, you've seen this. But remember, this is a reference type. So, remember, this is a reference type. So, remember, this is a reference type. So, what's going to happen here? what's going to happen here? what's going to happen here? We got true, true, and true, We got true, true, and true, We got true, true, and true, which is interesting. Okay, because which is interesting. Okay, because which is interesting. Okay, because we've now just broken something. I'm we've now just broken something. I'm we've now just broken something. I'm going to show you why. going to show you why. going to show you why. Okay, so equals well, these are two Okay, so equals well, these are two Okay, so equals well, these are two different variables up here. They were different variables up here. They were different variables up here. They were two different variables essentially two two different variables essentially two two different variables essentially two different uh pointers, two different different uh pointers, two different different uh pointers, two different locations in memory. So why is it that locations in memory. So why is it that locations in memory. So why is it that these two are equals? Well, because the these two are equals? Well, because the these two are equals? Well, because the how is going to work with a string. So how is going to work with a string. So how is going to work with a string. So this is actually a content comparison this is actually a content comparison this is actually a content comparison which is an override. which is an override. which is an override. I'm sorry overload.

  10. This is comparing the content This is comparing the content and because of an override and because of an override and because of an override and this is comparing the reference and this is comparing the reference and this is comparing the reference which again wait a minute two different which again wait a minute two different which again wait a minute two different places right? Well, yes, but strings are places right? Well, yes, but strings are places right? Well, yes, but strings are weird because what happens is that at weird because what happens is that at weird because what happens is that at runtime the runtime engine goes, hey, runtime the runtime engine goes, hey, runtime the runtime engine goes, hey, wait a minute. I have two strings which wait a minute. I have two strings which wait a minute. I have two strings which are both Tim. And so therefore what it are both Tim. And so therefore what it are both Tim. And so therefore what it says is you know what I can use a thing says is you know what I can use a thing says is you know what I can use a thing called interning where I can store the called interning where I can store the called interning where I can store the string value Tim in memory once and then string value Tim in memory once and then string value Tim in memory once and then I can point to it from two different I can point to it from two different I can point to it from two different locations name one and name two and so locations name one and name two and so locations name one and name two and so that's what it's doing here. So that's what it's doing here. So that's what it's doing here. So therefore the reference equal is going therefore the reference equal is going therefore the reference equal is going to be true because it's actually to be true because it's actually to be true because it's actually pointing to the same location. pointing to the same location. pointing to the same location. Now, let's break that apart a bit. So, Now, let's break that apart a bit. So, Now, let's break that apart a bit. So, what I'm going to do is I'm going to what I'm going to do is I'm going to what I'm going to do is I'm going to copy this right here, and we're going to copy this right here, and we're going to copy this right here, and we're going to come down here, and we're going to have come down here, and we're going to have come down here, and we're going to have name three. And let's just change over name three. And let's just change over name three. And let's just change over everything to be named three and break everything to be named three and break everything to be named three and break some things by instead of doing Tim as a some things by instead of doing Tim as a some things by instead of doing Tim as a string literal, I'm going to say new string literal, I'm going to say new string literal, I'm going to say new string Tim to char array, string Tim to char array, string Tim to char array, which all this does is it creates a which all this does is it creates a which all this does is it creates a string called Tim in here, but it kind string called Tim in here, but it kind string called Tim in here, but it kind of tricks things a little bit. How do

  11. of tricks things a little bit. How do of tricks things a little bit. How do you think these three are going to come you think these three are going to come you think these three are going to come out? Are they all going to be true? Well, true, true, and false. The Well, true, true, and false. The reference equal is now false. And this reference equal is now false. And this reference equal is now false. And this is one of the places where if you're not is one of the places where if you're not is one of the places where if you're not careful, you can trip up with strings careful, you can trip up with strings careful, you can trip up with strings because if you do reference equals, because if you do reference equals, because if you do reference equals, you'd think, well, always that Tim is you'd think, well, always that Tim is you'd think, well, always that Tim is going to be interned once, therefore going to be interned once, therefore going to be interned once, therefore we're good. Well, no. Technically, it's we're good. Well, no. Technically, it's we're good. Well, no. Technically, it's not. In fact, just to be really clear not. In fact, just to be really clear not. In fact, just to be really clear here, I'm going to put a break point here, I'm going to put a break point here, I'm going to put a break point right here. right here. right here. What is a name of three? Um, it's Tim or What is a name of three? Um, it's Tim or What is a name of three? Um, it's Tim or the value of three, it's Tim. Value of the value of three, it's Tim. Value of the value of three, it's Tim. Value of name two, Tim. Value of one, Tim. When name two, Tim. Value of one, Tim. When name two, Tim. Value of one, Tim. When you actually look at it, it's the same you actually look at it, it's the same you actually look at it, it's the same value. But because we went through it value. But because we went through it value. But because we went through it this way with this code, then it this way with this code, then it this way with this code, then it actually gets stored in memory in a actually gets stored in memory in a actually gets stored in memory in a different register. Therefore, we have a different register. Therefore, we have a different register. Therefore, we have a different pointer. Therefore, the different pointer. Therefore, the different pointer. Therefore, the reference equals are going to be false. reference equals are going to be false. reference equals are going to be false. So So So this is where what I'm trying to do here this is where what I'm trying to do here this is where what I'm trying to do here is I'm going to try and pull apart these is I'm going to try and pull apart these is I'm going to try and pull apart these different pieces so you understand oh I different pieces so you understand oh I different pieces so you understand oh I should be using this here and this there should be using this here and this there should be using this here and this there and I understand why double equals and I understand why double equals and I understand why double equals versus equals versus reference equals.

  12. versus equals versus reference equals. versus equals versus reference equals. So when we're talking about uh classes, So when we're talking about uh classes, So when we're talking about uh classes, reference equal works great because you reference equal works great because you reference equal works great because you can say, "Hey, are these two the same?" can say, "Hey, are these two the same?" can say, "Hey, are these two the same?" But it's also kind of the same thing as But it's also kind of the same thing as But it's also kind of the same thing as doing double equals or equals. Okay? But doing double equals or equals. Okay? But doing double equals or equals. Okay? But there are times when it might not be. there are times when it might not be. there are times when it might not be. All right? Because of the fact, we All right? Because of the fact, we All right? Because of the fact, we haven't talked about this. In fact, next haven't talked about this. In fact, next haven't talked about this. In fact, next week we're going to cover how to do week we're going to cover how to do week we're going to cover how to do this. But you can override the double this. But you can override the double this. But you can override the double equal and you can over I'm sorry, equal and you can over I'm sorry, equal and you can over I'm sorry, overload the double equal and you can overload the double equal and you can overload the double equal and you can override the equals method, which means override the equals method, which means override the equals method, which means that these two might not spit out what that these two might not spit out what that these two might not spit out what you expect for true and false for person you expect for true and false for person you expect for true and false for person model, but the reference equal will be model, but the reference equal will be model, but the reference equal will be the reference equal. Are these two the reference equal. Are these two the reference equal. Are these two references the same? I don't care if the references the same? I don't care if the references the same? I don't care if the equals is overridden or not. I don't equals is overridden or not. I don't equals is overridden or not. I don't care if uh the double equals is is over care if uh the double equals is is over care if uh the double equals is is over uh overloaded or not. Those two might uh overloaded or not. Those two might uh overloaded or not. Those two might change. Reference equal will not for change. Reference equal will not for change. Reference equal will not for reference types. Okay, which we've reference types. Okay, which we've reference types. Okay, which we've already seen that with the string already seen that with the string already seen that with the string because this is overloaded. This is because this is overloaded. This is because this is overloaded. This is overridden for string types. Okay. So overridden for string types. Okay. So overridden for string types. Okay. So now let's do one more.

  13. now let's do one more. now let's do one more. And we'll just copy this so we have And we'll just copy this so we have And we'll just copy this so we have something to work from instead of again something to work from instead of again something to work from instead of again me typing. Okay. I'm going to use a me typing. Okay. I'm going to use a me typing. Okay. I'm going to use a record type this time instead of a uh record type this time instead of a uh record type this time instead of a uh person model. So a class type. So person person model. So a class type. So person person model. So a class type. So person record. And did I not spell that right? Um per And did I not spell that right? Um per oh it's just messing up because of oh it's just messing up because of oh it's just messing up because of being the last line. Uh person record PR being the last line. Uh person record PR being the last line. Uh person record PR one equals new and we'll say Tim like one equals new and we'll say Tim like one equals new and we'll say Tim like so. so. so. Okay. Um and we're going to say person Okay. Um and we're going to say person Okay. Um and we're going to say person record two. And again just say Tim in record two. And again just say Tim in record two. And again just say Tim in there. Simple record. Okay. Now what there. Simple record. Okay. Now what there. Simple record. Okay. Now what we're going to do is we're going to we're going to do is we're going to we're going to do is we're going to print out these values. So, um, this is print out these values. So, um, this is print out these values. So, um, this is going to be the person record. Okay. And then we're going to compare Okay. And then we're going to compare PR1 and PR2.

  14. and PR2. Now, what do you think is going to be Now, what do you think is going to be Now, what do you think is going to be the results of these three? is person the results of these three? is person the results of these three? is person record record record one versus two double equals going to one versus two double equals going to one versus two double equals going to say true or false equals true or false say true or false equals true or false say true or false equals true or false and reference equals true or false. and reference equals true or false. and reference equals true or false. Let's run this. Let's run this. Let's run this. Okay, true true and false. So the way a Okay, true true and false. So the way a Okay, true true and false. So the way a record works is it overrides and record works is it overrides and record works is it overrides and overloads. So it overloads the double overloads. So it overloads the double overloads. So it overloads the double equals and it overrides I'm sorry equals and it overrides I'm sorry equals and it overrides I'm sorry overloads the equals and overrides overloads the equals and overrides overloads the equals and overrides Yeah. Uh, I got it written down here. Yeah. Uh, I got it written down here. Yeah. Uh, I got it written down here. Overload for double equals. How do I get Overload for double equals. How do I get Overload for double equals. How do I get those mixed up? An override for the those mixed up? An override for the those mixed up? An override for the equals. So, that's what's happening. And equals. So, that's what's happening. And equals. So, that's what's happening. And it still is doing a reference it still is doing a reference it still is doing a reference comparison. So, it's one of the benefits comparison. So, it's one of the benefits comparison. So, it's one of the benefits of records. I've got a video on records. of records. I've got a video on records. of records. I've got a video on records. Um, you can go back and watch. Um, Um, you can go back and watch. Um, Um, you can go back and watch. Um, records do a lot of things to make our records do a lot of things to make our records do a lot of things to make our lives easier. They they have a lot of lives easier. They they have a lot of lives easier. They they have a lot of different things that just make life different things that just make life different things that just make life easier. But just so you know when it easier. But just so you know when it easier. But just so you know when it comes to comparison they're comparing comes to comparison they're comparing comes to comparison they're comparing the values the content and they're the values the content and they're the values the content and they're comparing the content comparison is also comparing the content comparison is also comparing the content comparison is also in for equals not just for the double in for equals not just for the double in for equals not just for the double equals. So it's like a JSON string equals. So it's like a JSON string equals. So it's like a JSON string essentially essentially essentially for the two. So that's why when you look for the two. So that's why when you look for the two. So that's why when you look at a record, it works differently than a at a record, it works differently than a at a record, it works differently than a class, even though technically behind class, even though technically behind class, even though technically behind the scenes a record is a class, but the scenes a record is a class, but the scenes a record is a class, but there's just some extra work going on there's just some extra work going on there's just some extra work going on behind the scenes. Again, next week behind the scenes. Again, next week behind the scenes. Again, next week we're going to see more about how that

  15. we're going to see more about how that we're going to see more about how that work happens. So, um, definitely tune in work happens. So, um, definitely tune in work happens. So, um, definitely tune in for that or subscribe, you know, and for that or subscribe, you know, and for that or subscribe, you know, and that way you're going to get notified of that way you're going to get notified of that way you're going to get notified of new videos. Okay, one more to really new videos. Okay, one more to really new videos. Okay, one more to really just break everything. Let's go ahead just break everything. Let's go ahead just break everything. Let's go ahead and and grab these strings up here. and and grab these strings up here. and and grab these strings up here. And I am going to come down here and And I am going to come down here and And I am going to come down here and paste it. I'm not going to change paste it. I'm not going to change paste it. I'm not going to change anything except for the the type object. anything except for the the type object. anything except for the the type object. Of course, I have to change the name to Of course, I have to change the name to Of course, I have to change the name to match because otherwise we'll have a match because otherwise we'll have a match because otherwise we'll have a problem. Object one and object two. problem. Object one and object two. problem. Object one and object two. Remember that in uh C everything is an Remember that in uh C everything is an Remember that in uh C everything is an object. Everything derives from that. object. Everything derives from that. object. Everything derives from that. Therefore, um you could you know a Therefore, um you could you know a Therefore, um you could you know a string is an object. So you can kind of string is an object. So you can kind of string is an object. So you can kind of um have, you know, drop down to that, um have, you know, drop down to that, um have, you know, drop down to that, but not really. I'm going to show you but not really. I'm going to show you but not really. I'm going to show you why. So we're going to do comparisons. Okay. So remember that with strings, we Okay. So remember that with strings, we had content here, compared. So Tim had content here, compared. So Tim had content here, compared. So Tim versus Tim, which means true. We had versus Tim, which means true. We had versus Tim, which means true. We had equality here, which meant um again equality here, which meant um again equality here, which meant um again content. So therefore Tim versus tomb content. So therefore Tim versus tomb content. So therefore Tim versus tomb equals true and reference was false equals true and reference was false equals true and reference was false because they're two different loca well because they're two different loca well because they're two different loca well reference was true because stored they reference was true because stored they reference was true because stored they were interned together but it was false were interned together but it was false were interned together but it was false when we did this. Okay so what do you when we did this. Okay so what do you when we did this. Okay so what do you think this is going to be? Is there any think this is going to be? Is there any think this is going to be? Is there any changes or not? This is where kind of

  16. changes or not? This is where kind of changes or not? This is where kind of again kind of break things. Um equal again kind of break things. Um equal again kind of break things. Um equal equals is true equals is true reference equals is true equals is true reference equals is true equals is true reference equals is true. Okay. So, reference equals is true. Okay. So, reference equals is true. Okay. So, reference equals was false up or true up here. equals was false up or true up here. equals was false up or true up here. It's true down here. So, that works the It's true down here. So, that works the It's true down here. So, that works the same way as a string, right? Yes. But same way as a string, right? Yes. But same way as a string, right? Yes. But let's make a change. Let's grab this let's make a change. Let's grab this let's make a change. Let's grab this code right here and I'll just override code right here and I'll just override code right here and I'll just override this value. So, again, this comparison this value. So, again, this comparison this value. So, again, this comparison here, is anything going to change? Yes. Our double equals is now false. Yes. Our double equals is now false. So why is our double equals false? And So why is our double equals false? And So why is our double equals false? And again, if we come up here, I believe again, if we come up here, I believe again, if we come up here, I believe it's this one right here. It was true. it's this one right here. It was true. it's this one right here. It was true. So why is it false? Now, why is it So why is it false? Now, why is it So why is it false? Now, why is it false? We just use objects instead of false? We just use objects instead of false? We just use objects instead of strings. And this is where kind of a strings. And this is where kind of a strings. And this is where kind of a hidden bug comes in with double equals. hidden bug comes in with double equals. hidden bug comes in with double equals. So double equals, which is the operator, So double equals, which is the operator, So double equals, which is the operator, is a comparison at compile time for is a comparison at compile time for is a comparison at compile time for types.

  17. types. types. So what it's doing is saying objects are So what it's doing is saying objects are So what it's doing is saying objects are they equal? Well, object doesn't have an they equal? Well, object doesn't have an they equal? Well, object doesn't have an override for double equal. So therefore override for double equal. So therefore override for double equal. So therefore um and again I'm pricing it wrong. It's um and again I'm pricing it wrong. It's um and again I'm pricing it wrong. It's overload. Sorry. Um double equal for overload. Sorry. Um double equal for overload. Sorry. Um double equal for object doesn't have an overload. object doesn't have an overload. object doesn't have an overload. So therefore it does a comparison of So therefore it does a comparison of So therefore it does a comparison of reference types. reference types. reference types. Whereas the equals this is a runtime Whereas the equals this is a runtime Whereas the equals this is a runtime check of the types and so it finds check of the types and so it finds check of the types and so it finds string. Therefore it looks at the the string. Therefore it looks at the the string. Therefore it looks at the the over uh ridden equals and says okay that over uh ridden equals and says okay that over uh ridden equals and says okay that the string has an override for equals. the string has an override for equals. the string has an override for equals. Therefore use that therefore we get Therefore use that therefore we get Therefore use that therefore we get true. true. true. So and of course the reference equals So and of course the reference equals So and of course the reference equals false because they're in different false because they're in different false because they're in different different memory registers. So, [snorts] different memory registers. So, [snorts] different memory registers. So, [snorts] this is where we get kind of a bug. It this is where we get kind of a bug. It this is where we get kind of a bug. It feels like with double equals. It's not feels like with double equals. It's not feels like with double equals. It's not a bug. It's just how we're dealing with a bug. It's just how we're dealing with a bug. It's just how we're dealing with the the double equals versus the equals the the double equals versus the equals the the double equals versus the equals method and how it works with compile method and how it works with compile method and how it works with compile time versus runtime checks. If we are time versus runtime checks. If we are time versus runtime checks. If we are dealing with objects that are actually dealing with objects that are actually dealing with objects that are actually something else like strings, if we're something else like strings, if we're something else like strings, if we're expecting to act like a string, we need expecting to act like a string, we need expecting to act like a string, we need to make sure it's explicitly a string to make sure it's explicitly a string to make sure it's explicitly a string type. Otherwise, what happens is it type. Otherwise, what happens is it type. Otherwise, what happens is it takes a type that it is, which is an takes a type that it is, which is an takes a type that it is, which is an object, and goes, okay, I'll use that object, and goes, okay, I'll use that object, and goes, okay, I'll use that double equals. Whereas, and that's at

  18. double equals. Whereas, and that's at double equals. Whereas, and that's at compile time, at runtime, compile time, at runtime, compile time, at runtime, the equals method figures out what the the equals method figures out what the the equals method figures out what the type is and goes, okay, let's use that type is and goes, okay, let's use that type is and goes, okay, let's use that method, if it's overridden or not, with method, if it's overridden or not, with method, if it's overridden or not, with find the right one, and we'll run that find the right one, and we'll run that find the right one, and we'll run that one for the equals. one for the equals. one for the equals. So, that's where we get some some So, that's where we get some some So, that's where we get some some squiriness in how this works. So, here's squiriness in how this works. So, here's squiriness in how this works. So, here's the the bottom line of this. By the way, the the bottom line of this. By the way, the the bottom line of this. By the way, um we've got the this is going to be a um we've got the this is going to be a um we've got the this is going to be a reference type now reference comparison. reference type now reference comparison. reference type now reference comparison. Okay. Okay. Okay. Now, just for fun, let's do one more Now, just for fun, let's do one more Now, just for fun, let's do one more before we're done because I do want to before we're done because I do want to before we're done because I do want to point out one more difference between point out one more difference between point out one more difference between double equals and equals method. So, I'm double equals and equals method. So, I'm double equals and equals method. So, I'm going to grab um we can just reuse this going to grab um we can just reuse this going to grab um we can just reuse this right here. I'm going to copy it and right here. I'm going to copy it and right here. I'm going to copy it and come down here. But I'm going to say come down here. But I'm going to say come down here. But I'm going to say since they've already been declared, I'm since they've already been declared, I'm since they've already been declared, I'm gonna say P1 equals null. Now, just so gonna say P1 equals null. Now, just so gonna say P1 equals null. Now, just so you know, P1 is not marked as nullable, you know, P1 is not marked as nullable, you know, P1 is not marked as nullable, but it will still work because reference but it will still work because reference but it will still work because reference types can be null even if they're not types can be null even if they're not types can be null even if they're not marked as nullable, which is what it's marked as nullable, which is what it's marked as nullable, which is what it's about right here. Um, so we've got this about right here. Um, so we've got this about right here. Um, so we've got this comparison here, right? So, is this comparison here, right? So, is this comparison here, right? So, is this going to work? Right? Well, what do you going to work? Right? Well, what do you going to work? Right? Well, what do you think? Well, first of all, there's a think? Well, first of all, there's a think? Well, first of all, there's a little clue right here. Um, maybe null little clue right here. Um, maybe null little clue right here. Um, maybe null here, but let's find out. So, if we run here, but let's find out. So, if we run here, but let's find out. So, if we run this, we get a crash. Not on the first this, we get a crash. Not on the first this, we get a crash. Not on the first line, though. If we come back over here,

  19. line, though. If we come back over here, line, though. If we come back over here, we look, person model values are equal we look, person model values are equal we look, person model values are equal using double equals. using double equals. using double equals. So the double equals operator can So the double equals operator can So the double equals operator can compare null values, but the equals compare null values, but the equals compare null values, but the equals method can't handle the idea of having method can't handle the idea of having method can't handle the idea of having the um the this object be a null value the um the this object be a null value the um the this object be a null value or null reference because the fact that or null reference because the fact that or null reference because the fact that dot on a null just doesn't work. So dot on a null just doesn't work. So dot on a null just doesn't work. So you'd have to say something like, well, you'd have to say something like, well, you'd have to say something like, well, if it's not null, but then you got to do if it's not null, but then you got to do if it's not null, but then you got to do a comparison, a check to to say, well, a comparison, a check to to say, well, a comparison, a check to to say, well, that just doesn't print anything out. that just doesn't print anything out. that just doesn't print anything out. And by the way, if we just did that, it And by the way, if we just did that, it And by the way, if we just did that, it won't print anything out there. Um, but won't print anything out there. Um, but won't print anything out there. Um, but the reference types are equal the reference types are equal the reference types are equal because null is null. It's it has no uh because null is null. It's it has no uh because null is null. It's it has no uh location. Therefore, they're both the location. Therefore, they're both the location. Therefore, they're both the same because they have no location. So, same because they have no location. So, same because they have no location. So, just so you know that there's also that just so you know that there's also that just so you know that there's also that kind of squirly one as well. So, let's kind of squirly one as well. So, let's kind of squirly one as well. So, let's look at all three of these again now look at all three of these again now look at all three of these again now that we've seen kind of the different that we've seen kind of the different that we've seen kind of the different ways that things can can happen. Okay, ways that things can can happen. Okay, ways that things can can happen. Okay, so double equal which is a is an so double equal which is a is an so double equal which is a is an operator and this is going to do a operator and this is going to do a operator and this is going to do a comparison typically a reference comparison typically a reference comparison typically a reference comparison but it can be overloaded to comparison but it can be overloaded to comparison but it can be overloaded to do a content comparison or a value do a content comparison or a value do a content comparison or a value comparison and it does a value comparison and it does a value comparison and it does a value comparison on uh the value types.

  20. comparison on uh the value types. comparison on uh the value types. Okay. And it does handle null just fine, Okay. And it does handle null just fine, Okay. And it does handle null just fine, but it's also a check that is determined but it's also a check that is determined but it's also a check that is determined at compile time, which we saw down here at compile time, which we saw down here at compile time, which we saw down here with our um our objects where at compile with our um our objects where at compile with our um our objects where at compile time it's comparing these as objects. time it's comparing these as objects. time it's comparing these as objects. Therefore, it's a reference comparison. Therefore, it's a reference comparison. Therefore, it's a reference comparison. Okay. So then we have the equals and the Okay. So then we have the equals and the Okay. So then we have the equals and the equals does typically a a comparison of equals does typically a a comparison of equals does typically a a comparison of either the content or the reference either the content or the reference either the content or the reference [clears throat] depending on again uh [clears throat] depending on again uh [clears throat] depending on again uh value type versus reference type. So value type versus reference type. So value type versus reference type. So that's kind of a similar thing but it is that's kind of a similar thing but it is that's kind of a similar thing but it is a method which means it can be a method which means it can be a method which means it can be overridden uh as compared to overloaded overridden uh as compared to overloaded overridden uh as compared to overloaded the double equal. Uh so both can be the double equal. Uh so both can be the double equal. Uh so both can be changed and this is used to do a changed and this is used to do a changed and this is used to do a comparison for reference types or it can comparison for reference types or it can comparison for reference types or it can overridden for content type like in overridden for content type like in overridden for content type like in string or down here in record. So we string or down here in record. So we string or down here in record. So we have both types with this as well. Um have both types with this as well. Um have both types with this as well. Um this is a runtime check though which is this is a runtime check though which is this is a runtime check though which is something that helped us out here with something that helped us out here with something that helped us out here with our objects which were strings because our objects which were strings because our objects which were strings because at runtime it said hey that's a string at runtime it said hey that's a string at runtime it said hey that's a string therefore we can do equals which is the therefore we can do equals which is the therefore we can do equals which is the overridden version of equals on string overridden version of equals on string overridden version of equals on string therefore um we can we can show that as therefore um we can we can show that as therefore um we can we can show that as being equal um even though this is a

  21. being equal um even though this is a being equal um even though this is a reference check so it is a bit different reference check so it is a bit different reference check so it is a bit different which is why I typically say when you're which is why I typically say when you're which is why I typically say when you're doing string comparison to do equals um doing string comparison to do equals um doing string comparison to do equals um as opposed to the double equals because as opposed to the double equals because as opposed to the double equals because of the fact that this bug right here of the fact that this bug right here of the fact that this bug right here with object can bite you. Okay, so make with object can bite you. Okay, so make with object can bite you. Okay, so make sure that if you're you know you're sure that if you're you know you're sure that if you're you know you're doing strings there is a improvement by doing strings there is a improvement by doing strings there is a improvement by doing equals as compared to the double doing equals as compared to the double doing equals as compared to the double equals um because the double equals is equals um because the double equals is equals um because the double equals is uh overloaded with strings. But if it uh overloaded with strings. But if it uh overloaded with strings. But if it doesn't if it's not actually a string doesn't if it's not actually a string doesn't if it's not actually a string type, if it's an object type, it can type, if it's an object type, it can type, if it's an object type, it can look at the reference instead of the look at the reference instead of the look at the reference instead of the value. Okay, so that's the double equals value. Okay, so that's the double equals value. Okay, so that's the double equals versus the equals. The reference equal versus the equals. The reference equal versus the equals. The reference equal looks at the reference of the two looks at the reference of the two looks at the reference of the two objects where they are uh what memory objects where they are uh what memory objects where they are uh what memory registers are in and then it compares registers are in and then it compares registers are in and then it compares those memory registers to see if those those memory registers to see if those those memory registers to see if those two are equal, which is why it gets two are equal, which is why it gets two are equal, which is why it gets around a lot of problems with around a lot of problems with around a lot of problems with overriding. overriding. overriding. The only problem is it doesn't do value The only problem is it doesn't do value The only problem is it doesn't do value types like integer because it actually types like integer because it actually types like integer because it actually does boxing for that.

  22. does boxing for that. does boxing for that. So that's how equality works in C. So that's how equality works in C. So that's how equality works in C. That's a lot of words and a lot of tests That's a lot of words and a lot of tests That's a lot of words and a lot of tests to say this. Um you should know how this to say this. Um you should know how this to say this. Um you should know how this works. You should understand the works. You should understand the works. You should understand the differences which helps that you differences which helps that you differences which helps that you understand value types versus reference understand value types versus reference understand value types versus reference types. Um and then you should be careful types. Um and then you should be careful types. Um and then you should be careful which one you use based on the which one you use based on the which one you use based on the circumstances. So you don't introduce circumstances. So you don't introduce circumstances. So you don't introduce weird bugs because for the most part weird bugs because for the most part weird bugs because for the most part double equal and the equals method are double equal and the equals method are double equal and the equals method are going to work the exact same way. For going to work the exact same way. For going to work the exact same way. For the most part it's just for these these the most part it's just for these these the most part it's just for these these weird edge cases weird edge cases weird edge cases where you might have a difference where you might have a difference where you might have a difference especially this one down here where especially this one down here where especially this one down here where these two are different because actually these two are different because actually these two are different because actually the uh compile time versus runtime the uh compile time versus runtime the uh compile time versus runtime difference. So just note that um they difference. So just note that um they difference. So just note that um they are similar and both of them can be um are similar and both of them can be um are similar and both of them can be um changed. Let's put it that way. Uh changed. Let's put it that way. Uh changed. Let's put it that way. Uh double equals can be overloaded. The the double equals can be overloaded. The the double equals can be overloaded. The the equals method can be overridden. And equals method can be overridden. And equals method can be overridden. And again next week we're going to see how again next week we're going to see how again next week we're going to see how to do both and why you might want to do to do both and why you might want to do to do both and why you might want to do um overloading and overriding of these um overloading and overriding of these um overloading and overriding of these uh for different circumstances. even see uh for different circumstances. even see uh for different circumstances. even see how how that works with things like how how that works with things like how how that works with things like lists and etc. So that's equality in C.

  23. lists and etc. So that's equality in C. lists and etc. So that's equality in C. Yeah, it's a lot and you could probably Yeah, it's a lot and you could probably Yeah, it's a lot and you could probably get by without knowing this for 99% of get by without knowing this for 99% of get by without knowing this for 99% of cases, but when you start getting into cases, but when you start getting into cases, but when you start getting into weird bugs and and figuring out why weird bugs and and figuring out why weird bugs and and figuring out why things don't work, it might be quality things don't work, it might be quality things don't work, it might be quality and it might be a misunderstanding. It's and it might be a misunderstanding. It's and it might be a misunderstanding. It's also really important to understand also really important to understand also really important to understand reference types versus value types for reference types versus value types for reference types versus value types for this type of thing to understand what this type of thing to understand what this type of thing to understand what you're actually comparing. That way you you're actually comparing. That way you you're actually comparing. That way you don't get confused why um these two are don't get confused why um these two are don't get confused why um these two are not equal when it looks like they're the not equal when it looks like they're the not equal when it looks like they're the same thing. Okay. So um and I'm not sure same thing. Okay. So um and I'm not sure same thing. Okay. So um and I'm not sure I think the code comments's wrong here. I think the code comments's wrong here. I think the code comments's wrong here. I'm going to go ahead and No, it's I'm going to go ahead and No, it's I'm going to go ahead and No, it's person record. That's why. um why the person record. That's why. um why the person record. That's why. um why the person um I meant the person model say person um I meant the person model say person um I meant the person model say why these two look the same and yet they why these two look the same and yet they why these two look the same and yet they aren't compared to these two look the aren't compared to these two look the aren't compared to these two look the same and they are the same. So you same and they are the same. So you same and they are the same. So you should understand what this all means, should understand what this all means, should understand what this all means, how it all works. um so you don't get how it all works. um so you don't get how it all works. um so you don't get surprised in your code or you this is surprised in your code or you this is surprised in your code or you this is one that gets that bites you a lot is one that gets that bites you a lot is one that gets that bites you a lot is you start doing comparison in these two you start doing comparison in these two you start doing comparison in these two things um which are two references and things um which are two references and things um which are two references and you have weird bugs in your code. So be you have weird bugs in your code. So be you have weird bugs in your code. So be careful with it. Make sure you careful with it. Make sure you careful with it. Make sure you understand the three. And thanks for understand the three. And thanks for understand the three. And thanks for watching. And as always, I am Tim Corey.

  24. watching. And as always, I am Tim Corey. watching. And as always, I am Tim Corey. [music]

No summary available yet.

View original episode ↗