{"id":94769,"date":"2023-08-13T08:00:41","date_gmt":"2023-08-13T06:00:41","guid":{"rendered":"https:\/\/code-maze.com\/?p=94769"},"modified":"2023-08-13T09:08:07","modified_gmt":"2023-08-13T07:08:07","slug":"csharp-equality-operator-and-equals-method","status":"publish","type":"post","link":"https:\/\/code-maze.com\/csharp-equality-operator-and-equals-method\/","title":{"rendered":"Differences Between Equality Operator (==) And Equals Method In C#"},"content":{"rendered":"<p>In this article, we delve into the similarities and differences between the equality (<code>==<\/code>) operator and the Equals method in C#.<\/p>\n<div style=\"padding: 20px; border-left: 5px #dc2323 solid; display: block; margin-bottom: 20px; box-shadow: 1px 1px 5px 0px lightgrey;\">To download the source code for this article, you can visit our <a href=\"https:\/\/github.com\/CodeMazeBlog\/CodeMazeGuides\/tree\/main\/csharp-basic-topics\/EqualityOperatorVsEqualsMethodInCSharp\" target=\"_blank\" rel=\"nofollow noopener\">GitHub repository<\/a>.<\/div>\n<p>Now, let&#8217;s explore these concepts further!<\/p>\n<h2><a id=\"behaviourInDifferentScenarios\"><\/a>Equality Operator And Equals Method in Different Scenarios<\/h2>\n<p>Let&#8217;s review some examples to understand how\u00a0<code>==<\/code> operator and <code>Equals<\/code> method behaves in different cases. For a comprehensive go-through, we will also be exploring the usage of the <code>ReferenceEquals<\/code> method in the examples.<\/p>\n<h3><a id=\"builtInValueTypes\"><\/a>Built-In Value Types<\/h3>\n<p>Built-in value types, such as <code>int<\/code>, <code>float<\/code>, <code>bool<\/code>, and others, utilize the equality (<code>==<\/code>) operator to directly compare their values. The equality operator examines whether the values are equal or not.<\/p>\n<p>Before proceeding with the code examples, let&#8217;s take a moment to inspect a method that we will use in our snippets for printing our results:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public static string PrintFormattedResult(bool? a, bool? b, bool? c)\r\n{\r\n    var result = $\"Reference: {a}, Equality: {b}, Equals: {c}\";\r\n    Console.WriteLine(result);\r\n\r\n    return result;\r\n}<\/pre>\n<p>Taking that into account, let&#8217;s analyze a code snippet that compares the behaviors of the <code>ReferenceEquals<\/code> method, the equality (<code>==<\/code>) operator, and the <code>Equals<\/code> method:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">int firstNumber = 5;\r\nint secondNumber = 5;\r\n\r\nreturn PrintFormattedResult(\r\n    ReferenceEquals(firstNumber, secondNumber), \/\/ false\r\n    firstNumber == secondNumber, \/\/ true\r\n    firstNumber.Equals(secondNumber) \/\/ true\r\n);<\/pre>\n<p><strong>We must keep in mind that the<\/strong> <code>ReferenceEquals<\/code><strong> method cannot be utilized with value types and will consistently return <code>false<\/code> when invoked with value types.<\/strong> However, for built-in value types, both the <code>==<\/code> operator and the <code>Equals<\/code> method behaves similarly by returning <code>true<\/code> when the values of the operands are equal. Therefore, when comparing built-in value types, we can see that the <code>==<\/code> operator and the <code>Equals<\/code> method exhibit similar behavior.<\/p>\n<h3><a id=\"objects\"><\/a>Objects<\/h3>\n<p>The equality (<code>==<\/code>) operator exhibits different behavior for reference types, such as objects, by default. It checks for reference equality, comparing the memory addresses of the objects to determine if they are the same instance. Even if two different objects have the same content, they will not be considered equal with the <code>==<\/code> operator unless they refer to the same instance. Similarly, the <code>Equals<\/code> method, by default, performs reference equality comparisons for reference types.<\/p>\n<p>Let&#8217;s explore how two objects behave when we compare them using the <code>==<\/code> operator and the <code>Equals<\/code> method:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">object firstObject = new[] { 1, 2, 3 };\r\nobject secondObject = new[] { 1, 2, 3 };\r\n\r\nreturn PrintFormattedResult(\r\n    ReferenceEquals(firstObject, secondObject), \/\/ false\r\n    firstObject == secondObject, \/\/ false\r\n    firstObject.Equals(secondObject) \/\/ false\r\n);<\/pre>\n<p>The <code>ReferenceEquals<\/code> method returns <code>false<\/code> because these two objects have different memory addresses. The equality (<code>==<\/code>) operator also returns <code>false<\/code> for the same reason. Though the contents of both objects are the same, the <code>Equals<\/code> method returns <code>false<\/code> as the references of both objects are not identical.<\/p>\n<h3><a id=\"strings\"><\/a>Strings<\/h3>\n<p>Now, let&#8217;s study the behavior of the <code>==<\/code> operator and the <code>Equals<\/code> method when the comparison is made between two strings:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">string firstWord = \"pqr\";\r\nstring secondWord = \"pqr\";\r\n\r\nreturn\u00a0PrintFormattedResult(\r\n    ReferenceEquals(firstWord, secondWord), \/\/ true\r\n    firstWord == secondWord, \/\/ true\r\n    firstWord.Equals(secondWord) \/\/ true\r\n);<\/pre>\n<p>String interning in .NET causes the <code>ReferenceEquals<\/code> method to return <code>true<\/code>. <strong>String interning is a process where the runtime maintains a pool of unique string instances. When creating a string, the runtime actively checks if an identical string already exists in the pool. If it does, the new string reference points to the existing string instance instead of creating a new one<\/strong>.<\/p>\n<p>C# performs this optimization for conserving memory and improving performance.<span style=\"color: #ff0000;\">\u00a0<\/span>For <code>string<\/code>, the equality (<code>==<\/code>) operator, inequality (<code>!=<\/code>) operator and the <code>Equals<\/code> method perform a case-sensitive, ordinal comparison by default. Hence, both the <code>==<\/code> operator and the <code>Equals<\/code> method returns <code>true<\/code> when a comparison is made between <code>firstWord<\/code> and\u00a0<code>secondWord<\/code>. Besides, we can pass a <code>StringComparison<\/code> argument with the <code>Equals<\/code> method for altering its sorting rules. To learn more about this, you may visit <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/csharp\/how-to\/compare-strings#default-ordinal-comparisons\" target=\"_blank\" rel=\"nofollow noopener\">Default ordinal comparisons<\/a>.\u00a0<\/p>\n<h3><a id=\"objectAndString\"><\/a>Object and String<\/h3>\n<p>With that in mind, let&#8217;s take a closer look at how the <code>==<\/code> operator and the <code>Equals<\/code> method behave when we compare an <code>object<\/code> with a <code>string<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">object thirdObject = new string(new[] { 'x', 'y', 'z' });\r\nstring thirdWord = \"xyz\";\r\n\r\nreturn\u00a0PrintFormattedResult(\r\n    ReferenceEquals(thirdObject, thirdWord), \/\/ false\r\n    thirdObject == thirdWord, \/\/ false\r\n    thirdObject.Equals(thirdWord) \/\/ true\r\n);<\/pre>\n<p>The <code>ReferenceEquals<\/code> method returns <code>false<\/code> because <code>thirdObject<\/code> and <code>thirdWord<\/code> have different memory references. We can see that <code>thirdObject<\/code> is a <code>string<\/code> boxed as an <code>object<\/code> and <code>thirdWord<\/code> is simply a <code>string<\/code>. When we compare these two, the <code>==<\/code> operator defined on the <code>object<\/code> gets invoked, rather than the <code>string<\/code>. This leads to a reference comparison and a <code>false<\/code> value is returned. To achieve the behavior of <code>==<\/code> operator defined on the <code>string<\/code> class, we have to cast the <code>thirdObject<\/code> to a <code>string<\/code>:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">Console.WriteLine((string) thirdObject == thirdWord); \/\/ true<\/code><\/p>\n<p>We get a return value of <code>true<\/code> once we cast the <code>thirdObject<\/code> to <code>string<\/code> and compare it with <code>thirdWord<\/code>.<\/p>\n<p>Finally, the <code>thirdObject<\/code> being a <code>string<\/code> calls the override of the <code>Equals<\/code> method defined in the\u00a0<code>string<\/code> class, which performs the default case-sensitive ordinal comparison. This allows for an accurate comparison between the two string values.<\/p>\n<h3><a id=\"classes\"><\/a>Classes<\/h3>\n<p>For user-defined types (e.g., classes, structs, and records), the behavior of the <code>==<\/code> operator depends on how equality is defined for the type. By default, the <code>==<\/code> operator compares reference equality for classes. <strong>However, we can override the <code>==<\/code> operator or implement the <code>Equals()<\/code> method to define custom equality comparison logic for our types<\/strong>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var firstEmployee = new Employee(\"Hermione Granger\", 5000);\r\nvar secondEmployee = new Employee(\"Hermione Granger\", 5000);\r\n\r\nreturn PrintFormattedResult(\r\n    ReferenceEquals(firstEmployee, secondEmployee), \/\/ false\r\n    firstEmployee == secondEmployee, \/\/ false\r\n    firstEmployee.Equals(secondEmployee) \/\/ false\r\n);<\/pre>\n<p>The <code>ReferenceEquals<\/code> method returns <code>false<\/code> because <code>firstEmployee<\/code> and <code>secondEmployee<\/code> have different memory addresses. The equality (<code>==<\/code>) operator also performs reference equality for class types and hence returns <code>false<\/code>. Lastly, the <code>Equals<\/code> method defaults to performing reference equality, resulting in a <code>false<\/code> return. <strong>Again, it is possible to override the <code>Equals<\/code> method to enable a more meaningful comparison of equality<\/strong>.<\/p>\n<h3><a id=\"structs\"><\/a>Structs<\/h3>\n<p>That being said, let&#8217;s analyze the behavior of the <code>==<\/code> operator and the <code>Equals<\/code> method when the comparison is made between two struct types:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var firstCar = new Car(\"Audi R8\", 3715);\r\nvar secondCar = new Car(\"Audi R8\", 3715);\r\n\r\nreturn PrintFormattedResult(\r\n    ReferenceEquals(firstCar, secondCar), \/\/ false\r\n    firstCar == secondCar, \/\/ true\r\n    firstCar.Equals(secondCar) \/\/ true\r\n);<\/pre>\n<p>In C#, structs are value types, which means they store their instances directly with their values instead of referring to memory locations. When using the <code>ReferenceEquals<\/code> method to compare two struct instances, it will always return <code>false<\/code> because the method verifies if the two references point to the same memory location, which is not applicable for structs.<\/p>\n<p><strong>Next, we encounter the <code>==<\/code> operator, which is not automatically defined for struct types<\/strong>. As a result, we have to <a href=\"https:\/\/code-maze.com\/csharp-operator-overloading\/\" target=\"_blank\" rel=\"noopener\">override<\/a> the <code>==<\/code> operator on the <code>Car<\/code> class in a way that replicates the behavior of the <code>Equals<\/code> method:\u00a0<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public override bool Equals(object? obj)\r\n{\r\n    if (obj is not Car other) return false;\r\n\r\n    return Model == other.Model &amp;&amp; Weight == other.Weight;\r\n}\r\n\r\npublic override int GetHashCode() =&gt; (Model, Weight).GetHashCode();\r\n\r\npublic static bool operator ==(Car carLeft, Car carRight) =&gt; carLeft.Equals(carRight);\r\n\r\npublic static bool operator !=(Car carLeft, Car carRight) =&gt; !(carLeft == carRight);<\/pre>\n<p>Finally, the <code>Equals<\/code> method compares the values of each field in the struct to determine equality. Since all the fields have identical values, the method returns <code>true<\/code>.<\/p>\n<h3><a id=\"records\"><\/a>Records<\/h3>\n<p>With the <code>class<\/code> and <code>struct<\/code> types covered, let&#8217;s learn how the <code>==<\/code> operator and the <code>Equals<\/code> method behave when we compare two <code>record<\/code> types:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var firstLaptop = new Laptop(\"ASUS TUF A15\", 1499);\r\nvar secondLaptop = new Laptop(\"ASUS TUF A15\", 1499);\r\n\r\nreturn PrintFormattedResult(\r\n    ReferenceEquals(firstLaptop, secondLaptop), \/\/ false\r\n    firstLaptop == secondLaptop, \/\/ true\r\n    firstLaptop.Equals(secondLaptop) \/\/ true\r\n);<\/pre>\n<p>The <code>ReferenceEquals<\/code> method returns <code>false<\/code> because <code>firstLaptop<\/code> and <code>secondLaptop<\/code> have different memory addresses.\u00a0The equality (<code>==<\/code>) operator, by default, performs a memberwise comparison for record types. As all the members are identical, it returns <code>true<\/code> in this case. Lastly, the <code>Equals<\/code> method also has behavior similar to the equality (==) operator, and outputs will also be similar.<\/p>\n<h3><a id=\"nullableTypes\"><\/a>Nullable Types<\/h3>\n<p>When dealing with nullable types (e.g., <code>int?<\/code>, <code>float?<\/code>, etc.), we can use the equality (<code>==<\/code>) operator to compare nullable values. It returns <code>true<\/code> if both operands are <code>null<\/code> or have the same underlying value:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">int? firstNullableNumber = null;\r\nint? secondNullableNumber = null;\r\n\r\nreturn PrintFormattedResult(\r\n    ReferenceEquals(firstNullableNumber, secondNullableNumber), \/\/ true\r\n    firstNullableNumber == secondNullableNumber, \/\/ true\r\n    firstNullableNumber.Equals(secondNullableNumber) \/\/ true\r\n);<\/pre>\n<p>The <code>ReferenceEquals<\/code> method returns <code>true<\/code> here, both arguments are null, indicating that they refer to the same null reference. Next, the equality (<code>==<\/code>) operator evaluates to <code>true<\/code> because <code>null<\/code> represents the absence of a value, and two <code>null<\/code> values are considered equal. Finally, when both nullable instances have null values, the <code>Equals<\/code> method considers them equal because they represent the absence of a value.<\/p>\n<h2><a id=\"conclusion\"><\/a>Conclusion<\/h2>\n<p>In this article, we have learned about the different behaviors of the equality operator and the Equals method in various scenarios. Moreover, this knowledge will enable us to make more informed decisions regarding when to utilize the equality operator and the <code>Equals<\/code> method in our code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we delve into the similarities and differences between the equality (==) operator and the Equals method in C#. \u00a0 Now, let&#8217;s explore these concepts further! Equality Operator And Equals Method in Different Scenarios Let&#8217;s review some examples to understand how\u00a0== operator and Equals method behaves in different cases. For a comprehensive go-through, [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":62189,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[12],"tags":[1811,1521,1067],"class_list":["post-94769","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-csharp","tag-c","tag-equality-operator","tag-equals","et-has-post-format-content","et_post_format-et-post-format-standard"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Equality Operator (==) And Equals Method In C# - Code Maze<\/title>\n<meta name=\"description\" content=\"Learn the difference between the equality operator ( == ) and the Equals method in C# and the behavior of each in different scenarios\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/code-maze.com\/csharp-equality-operator-and-equals-method\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Equality Operator (==) And Equals Method In C# - Code Maze\" \/>\n<meta property=\"og:description\" content=\"Learn the difference between the equality operator ( == ) and the Equals method in C# and the behavior of each in different scenarios\" \/>\n<meta property=\"og:url\" content=\"https:\/\/code-maze.com\/csharp-equality-operator-and-equals-method\/\" \/>\n<meta property=\"og:site_name\" content=\"Code Maze\" \/>\n<meta property=\"article:published_time\" content=\"2023-08-13T06:00:41+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-08-13T07:08:07+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1100\" \/>\n\t<meta property=\"og:image:height\" content=\"620\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Code Maze\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/CodeMazeBlog\" \/>\n<meta name=\"twitter:site\" content=\"@CodeMazeBlog\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Code Maze\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":[\"Article\",\"BlogPosting\"],\"@id\":\"https:\/\/code-maze.com\/csharp-equality-operator-and-equals-method\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/csharp-equality-operator-and-equals-method\/\"},\"author\":{\"name\":\"Code Maze\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04\"},\"headline\":\"Differences Between Equality Operator (==) And Equals Method In C#\",\"datePublished\":\"2023-08-13T06:00:41+00:00\",\"dateModified\":\"2023-08-13T07:08:07+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/code-maze.com\/csharp-equality-operator-and-equals-method\/\"},\"wordCount\":1095,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/csharp-equality-operator-and-equals-method\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"keywords\":[\"C#\",\"equality operator\",\"Equals\"],\"articleSection\":[\"C#\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/code-maze.com\/csharp-equality-operator-and-equals-method\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/code-maze.com\/csharp-equality-operator-and-equals-method\/\",\"url\":\"https:\/\/code-maze.com\/csharp-equality-operator-and-equals-method\/\",\"name\":\"Equality Operator (==) And Equals Method In C# - Code Maze\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/code-maze.com\/csharp-equality-operator-and-equals-method\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/csharp-equality-operator-and-equals-method\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"datePublished\":\"2023-08-13T06:00:41+00:00\",\"dateModified\":\"2023-08-13T07:08:07+00:00\",\"description\":\"Learn the difference between the equality operator ( == ) and the Equals method in C# and the behavior of each in different scenarios\",\"breadcrumb\":{\"@id\":\"https:\/\/code-maze.com\/csharp-equality-operator-and-equals-method\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/code-maze.com\/csharp-equality-operator-and-equals-method\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/csharp-equality-operator-and-equals-method\/#primaryimage\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"width\":1100,\"height\":620,\"caption\":\"C# Development\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/code-maze.com\/csharp-equality-operator-and-equals-method\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/code-maze.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Differences Between Equality Operator (==) And Equals Method In C#\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/code-maze.com\/#website\",\"url\":\"https:\/\/code-maze.com\/\",\"name\":\"Code Maze\",\"description\":\"Learn. Code. Succeed.\",\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/code-maze.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/code-maze.com\/#organization\",\"name\":\"Code Maze\",\"url\":\"https:\/\/code-maze.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"width\":3511,\"height\":3510,\"caption\":\"Code Maze\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/CodeMazeBlog\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04\",\"name\":\"Code Maze\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png\",\"caption\":\"Code Maze\"},\"description\":\"This is the standard author on the site. Most articles are published by individual authors, with their profiles, but when several authors have contributed, we publish collectively as a part of this profile.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/company\/codemaze\/\",\"https:\/\/x.com\/https:\/\/twitter.com\/CodeMazeBlog\"],\"url\":\"https:\/\/code-maze.com\/author\/codemazecontributor\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Equality Operator (==) And Equals Method In C# - Code Maze","description":"Learn the difference between the equality operator ( == ) and the Equals method in C# and the behavior of each in different scenarios","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/code-maze.com\/csharp-equality-operator-and-equals-method\/","og_locale":"en_US","og_type":"article","og_title":"Equality Operator (==) And Equals Method In C# - Code Maze","og_description":"Learn the difference between the equality operator ( == ) and the Equals method in C# and the behavior of each in different scenarios","og_url":"https:\/\/code-maze.com\/csharp-equality-operator-and-equals-method\/","og_site_name":"Code Maze","article_published_time":"2023-08-13T06:00:41+00:00","article_modified_time":"2023-08-13T07:08:07+00:00","og_image":[{"width":1100,"height":620,"url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","type":"image\/png"}],"author":"Code Maze","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/CodeMazeBlog","twitter_site":"@CodeMazeBlog","twitter_misc":{"Written by":"Code Maze","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["Article","BlogPosting"],"@id":"https:\/\/code-maze.com\/csharp-equality-operator-and-equals-method\/#article","isPartOf":{"@id":"https:\/\/code-maze.com\/csharp-equality-operator-and-equals-method\/"},"author":{"name":"Code Maze","@id":"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04"},"headline":"Differences Between Equality Operator (==) And Equals Method In C#","datePublished":"2023-08-13T06:00:41+00:00","dateModified":"2023-08-13T07:08:07+00:00","mainEntityOfPage":{"@id":"https:\/\/code-maze.com\/csharp-equality-operator-and-equals-method\/"},"wordCount":1095,"commentCount":0,"publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"image":{"@id":"https:\/\/code-maze.com\/csharp-equality-operator-and-equals-method\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","keywords":["C#","equality operator","Equals"],"articleSection":["C#"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/code-maze.com\/csharp-equality-operator-and-equals-method\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/code-maze.com\/csharp-equality-operator-and-equals-method\/","url":"https:\/\/code-maze.com\/csharp-equality-operator-and-equals-method\/","name":"Equality Operator (==) And Equals Method In C# - Code Maze","isPartOf":{"@id":"https:\/\/code-maze.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/code-maze.com\/csharp-equality-operator-and-equals-method\/#primaryimage"},"image":{"@id":"https:\/\/code-maze.com\/csharp-equality-operator-and-equals-method\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","datePublished":"2023-08-13T06:00:41+00:00","dateModified":"2023-08-13T07:08:07+00:00","description":"Learn the difference between the equality operator ( == ) and the Equals method in C# and the behavior of each in different scenarios","breadcrumb":{"@id":"https:\/\/code-maze.com\/csharp-equality-operator-and-equals-method\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/code-maze.com\/csharp-equality-operator-and-equals-method\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/csharp-equality-operator-and-equals-method\/#primaryimage","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","width":1100,"height":620,"caption":"C# Development"},{"@type":"BreadcrumbList","@id":"https:\/\/code-maze.com\/csharp-equality-operator-and-equals-method\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/code-maze.com\/"},{"@type":"ListItem","position":2,"name":"Differences Between Equality Operator (==) And Equals Method In C#"}]},{"@type":"WebSite","@id":"https:\/\/code-maze.com\/#website","url":"https:\/\/code-maze.com\/","name":"Code Maze","description":"Learn. Code. Succeed.","publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/code-maze.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/code-maze.com\/#organization","name":"Code Maze","url":"https:\/\/code-maze.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","width":3511,"height":3510,"caption":"Code Maze"},"image":{"@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/CodeMazeBlog"]},{"@type":"Person","@id":"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04","name":"Code Maze","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/person\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png","caption":"Code Maze"},"description":"This is the standard author on the site. Most articles are published by individual authors, with their profiles, but when several authors have contributed, we publish collectively as a part of this profile.","sameAs":["https:\/\/www.linkedin.com\/company\/codemaze\/","https:\/\/x.com\/https:\/\/twitter.com\/CodeMazeBlog"],"url":"https:\/\/code-maze.com\/author\/codemazecontributor\/"}]}},"_links":{"self":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/94769","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/comments?post=94769"}],"version-history":[{"count":9,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/94769\/revisions"}],"predecessor-version":[{"id":94779,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/94769\/revisions\/94779"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media\/62189"}],"wp:attachment":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media?parent=94769"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/categories?post=94769"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/tags?post=94769"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}