{"id":64604,"date":"2022-02-14T07:28:19","date_gmt":"2022-02-14T06:28:19","guid":{"rendered":"https:\/\/drafts.code-maze.com\/?p=64604"},"modified":"2023-11-08T15:01:31","modified_gmt":"2023-11-08T14:01:31","slug":"csharp-operator-overloading","status":"publish","type":"post","link":"https:\/\/code-maze.com\/csharp-operator-overloading\/","title":{"rendered":"Operator Overloading in C#"},"content":{"rendered":"<p>In this article, we are going to learn about operator overloading in C#. It gives us various flexibility when we work with user-defined types.<\/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-operators\/OperatorOverloading\" target=\"_blank\" rel=\"nofollow noopener\">GitHub repository<\/a>.<\/div>\n<p>Let&#8217;s start.<\/p>\n<h2><a id=\"Defination\"><\/a>What Is Operator Overloading?<\/h2>\n<p>Operator overloading is a technique to redefine a built-in operator. C#, with the help of operator overloading, allows us to use the same built-in operators in different ways. We can build user-defined implementations of various operations where one or both of the operands are of the user-defined type.<\/p>\n<h3><a id=\"Rules\"><\/a>Rules for Operator Overloading<\/h3>\n<p>To overload an operator, we must use the <code>operator<\/code> keyword. We have to use both <code>public<\/code> and <code>static<\/code> modifiers in the declaration. The unary operator will have one and the binary operator will have two input parameters. At least one parameter must be of type <code>Type<\/code> or <code>Type?<\/code> in each case, where <code>Type<\/code> is the type containing the operator declaration.<\/p>\n<h2><a id=\"Overloadable\"><\/a>Can We Overload All Operators?<\/h2>\n<p>We can overload most of the operators in C#. <span style=\"font-weight: 400;\">But there are some operators that we can\u2019t overload and some that we can with certain conditions:<\/span><\/p>\n\n<table id=\"tablepress-22\" class=\"tablepress tablepress-id-22\">\n<thead>\n<tr class=\"row-1\">\n\t<th class=\"column-1\">Operators<\/th><th class=\"column-2\">Comments<\/th>\n<\/tr>\n<\/thead>\n<tbody class=\"row-striping row-hover\">\n<tr class=\"row-2\">\n\t<td class=\"column-1\">+, -, !, ~, ++, --, true, false<\/td><td class=\"column-2\">We can overload these unary operators.<\/td>\n<\/tr>\n<tr class=\"row-3\">\n\t<td class=\"column-1\">+, -, *, \/, %, &amp;, |, ^ (logical OR), &lt;&lt;, &gt;&gt;<\/td><td class=\"column-2\">We can overload these binary operators.<\/td>\n<\/tr>\n<tr class=\"row-4\">\n\t<td class=\"column-1\"> ==, !=, &lt;, &gt;, &lt;=, &gt;=<\/td><td class=\"column-2\">We can overload these binary relational operators. But we must overload them in pairs. So if we overload the <code>==<\/code> operator, we also need to overload the <code>!=<\/code> operator and vice versa. Other pairs are the <code>&lt;<\/code> and <code>&gt;<\/code> operators and the <code>&lt;=<\/code> and <code>&gt;=<\/code> operators.<\/td>\n<\/tr>\n<tr class=\"row-5\">\n\t<td class=\"column-1\">&amp;&amp;, ||<\/td><td class=\"column-2\">We can't overload these conditional logical operators directly.<\/td>\n<\/tr>\n<tr class=\"row-6\">\n\t<td class=\"column-1\">+=, -=, *=, \/=, %=, &amp;=, |=, ^=, &lt;&lt;=, &gt;&gt;=<\/td><td class=\"column-2\">We can't overload these compound assignment operators explicitly. But if we overload a binary operator, the corresponding compound assignment operator will also be overloaded indirectly. For example, the <code>+=<\/code> operator is evaluated using the <code>+<\/code> operator. So, if we overload the binary operator <code>+<\/code>, the <code>+=<\/code> operator will be implicitly overloaded.<\/td>\n<\/tr>\n<tr class=\"row-7\">\n\t<td class=\"column-1\">^ (Index from end operator), =, ., ?., ?:, ??, ??=, .., -&gt;, =&gt;, (), as, await, checked, unchecked, default, delegate, is, nameof, new, sizeof, stackalloc, switch, typeof, with<\/td><td class=\"column-2\">We can't overload these operators.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<!-- #tablepress-22 from cache -->\n<h2><a id=\"UOperator\"><\/a>Overloading Unary Operator<\/h2>\n<p>The return type of the unary operators (<code>+<\/code>, <code>-<\/code>, <code>!<\/code>, <code>~<\/code>) can be any type except the <code>void<\/code>. But for the <code>++<\/code>, <code>--<\/code> operators, the return type must be the <code>Type<\/code> that contains the unary operator declaration. For the <code>true<\/code> and <code>false<\/code> operators, it must be bool type. Let\u2019s inspect the syntax of overloading a unary operator:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public static returnType operator unaryOperator(Type t)\r\n{\r\n    \/\/body\r\n}\r\n<\/pre>\n<p>Here, the modifier is always <code>public<\/code> and <code>static<\/code> as we mentioned in the rules. The <code>operator<\/code> is a keyword that we need for operator overloading. <code>unaryOperator<\/code> will be replaced with our desired operator that we want to overload. The <code>Type<\/code> will be user-defined, may it be <code>class<\/code> or <code>struct<\/code>.<\/p>\n<p>Now, we are going to see how to overload a unary operator with an example. We will use the <code>Student<\/code> class throughout the article for this demonstration:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public class Student\r\n{\r\n    private readonly int _rollNo;\r\n    private readonly int _level;\r\n    private readonly string _name;\r\n    private readonly int _age;\r\n    private int _numberOfPassedCourses;\r\n\r\n    public Student(int rollNo, int level, string name, int age, int passedCourses)\r\n    {\r\n        _rollNo = rollNo;\r\n        _level = level;\r\n        _name = name;\r\n        _age = age;\r\n        _numberOfPassedCourses = passedCourses;\r\n    }\r\n\r\n    public int GetNumberOfPassedCourses() =&gt; _numberOfPassedCourses;\r\n\r\n    public string GetName() =&gt; _name;\r\n}<\/pre>\n<p>Let\u2019s consider that John is a student. He passed two courses. Recently he has appeared in a new course exam and has passed it successfully. So, his number of passed courses will be three. But we can\u2019t do that by just doing <code>john++<\/code>. Here we can use operator overloading to make our life easier. We can overload the <code>++<\/code> operator so that it works as we wish:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public static Student operator ++(Student student)\r\n{\r\n    student._numberOfPassedCourses++;\r\n    return student;\r\n}<\/pre>\n<p>We overload the <code>++<\/code> operator here. It takes the student as a parameter and increases the <code>_numberOfPassedCourses<\/code> value by one. Now we can use the <code>++<\/code> operator by calling <code>john++<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var john = new Student(1, 1, \"John\", 7, 2);\r\njohn++;\r\n\r\nConsole.WriteLine($\"{john.GetName()} has passed {john.GetNumberOfPassedCourses()} courses.\");<\/pre>\n<p>We can see the output as expected:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">John has passed 3 courses.<\/code><\/p>\n<h2><a id=\"BOperator\"><\/a>Overloading Binary Operator<\/h2>\n<p>The binary operator will always take two parameters as input and return any value except the void. Between two parameters, one must be a type <code>Type<\/code> that contains the operator declaration.<\/p>\n<p>Let\u2019s consider John and Alice. We can overload the <code>&gt;<\/code> operator in such a way that it will return who is older between the two. As the <code>&gt;<\/code> operator is a relational operator, we must overload the <code>&lt;<\/code> operator as well:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public static bool operator &lt;(Student studentLeft, Student studentRight)\r\n{\r\n    if (studentLeft._age &lt; studentRight._age)\r\n        return true;\r\n    else\r\n        return false;\r\n}\r\n\r\npublic static bool operator &gt;(Student studentLeft, Student studentRight)\r\n{\r\n    if (studentLeft._age &gt; studentRight._age)\r\n        return true;\r\n    else\r\n        return false;\r\n}<\/pre>\n<p>Now we can call the <code>&gt;<\/code> and <code>&lt;<\/code> operators directly over John and Alice:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var john = new Student(1, 1, \"John\", 7, 2);\r\nvar alice = new Student(1, 5, \"Alice\", 12, 5);\r\n\r\nConsole.WriteLine(john &gt; alice ? $\"{john.GetName()} is older than {alice.GetName()}.\" : $\"{john.GetName()} is younger than {alice.GetName()}.\");<\/pre>\n<p>And, we can see the output:<br \/>\n<code class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">John is younger than Alice.<\/code><\/p>\n<h2><a id=\"EOperator\"><\/a>Overloading <strong>Equality Operator<\/strong><\/h2>\n<p>When overloading the <code>==<\/code> operator, we need to overload the <code>!=<\/code> operator as well. Because both are relational operators, we need to overload them in pairs.<\/p>\n<p>We also need to override the <code>Equals<\/code> method here. The equality operator is intended to be a syntactically convenient way to access the functionality of the <code>Equals<\/code> method. So, the logic of the equality operator must be identical to that of the <code>Equals<\/code> method.<\/p>\n<p>For overriding the <code>Equals<\/code> method, it is important to override the <code>GetHashCode()<\/code> method. Because if two objects are the same, they must have the same hashcode. So, if we override the <code>Equals<\/code> method to make a particular comparison of two objects, and the method considers the two objects to be the same, then the hash code of the two objects must be the same as well.<\/p>\n<p>Now, the <code>Equals<\/code> method provides a reference-based comparison. We will override it so that it can give a value-based comparison. If the <code>_rollNo<\/code>, <code>_level<\/code>, and <code>_age<\/code> of two students are identical, we can assume that they indicate the same student. So, let\u2019s override the <code>Equals<\/code> and the <code>GetHashCode()<\/code> methods in the <code>Student<\/code> class:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public override bool Equals(object student)\r\n{\r\n    if (student == null)\r\n        return false;\r\n    if (GetType() != student.GetType())\r\n        return false;\r\n\r\n    var student1 = (Student)student;\r\n    return (_rollNo == student1._rollNo) &amp;&amp; (_level == student1._level) &amp;&amp; (_age == student1._age);\r\n}\r\n\r\npublic override int GetHashCode()\r\n{\r\n    return ToString().GetHashCode();\r\n}<\/pre>\n<p>After checking the <code>null<\/code> and the type, we compare the <code>_rollNo<\/code>, <code>_level<\/code>, and <code>_age<\/code> and return the result.<\/p>\n<p>Now let\u2019s overload <code>==<\/code> and <code>!=<\/code> operators so that the logic becomes identical to that of the <code>Equals<\/code> method:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public static bool operator ==(Student studentLeft, Student studentRight)\r\n{\r\n    if (studentLeft.Equals(studentRight))\r\n        return true;\r\n    else\r\n        return false;\r\n}\r\n\r\npublic static bool operator !=(Student studentLeft, Student studentRight)\r\n{\r\n    if (studentLeft.Equals(studentRight))\r\n        return false;\r\n    else\r\n        return true;\r\n}<\/pre>\n<p>So, both methods are using the logic of the <code>Equals<\/code> method.<\/p>\n<p>After that, let\u2019s use these overloaded operators:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var john = new Student(1, 1, \"John\", 7, 2);\r\nvar alice = new Student(1, 5, \"Alice\", 12, 5);\r\nvar johnSecond = new Student(1, 1, \"John\", 7, 2);\r\n\r\nConsole.WriteLine(john.Equals(johnSecond) ? \"Both indicate the same person.\" : \"They indicate different persons.\");\r\nConsole.WriteLine(john == johnSecond ? \"Both indicate the same person.\" : \"They indicate different persons.\");\r\n\r\njohnSecond++;\r\n\r\nConsole.WriteLine(john.Equals(johnSecond) ? \"Both indicate the same person.\" : \"They indicate different persons.\");\r\nConsole.WriteLine(john == johnSecond ? \"Both indicate the same person.\" : \"They indicate different persons.\");\r\nConsole.WriteLine(john != alice ? \"They indicate different persons.\" : \"Both indicate the same person.\");<\/pre>\n<p>Here we use the <code>Equals<\/code> method, and the <code>==<\/code> and <code>!=<\/code> operators to compare our objects based on <code>_rollNo<\/code>, <code>_level,<\/code> and <code>_age<\/code>. Then, we change the value of <code>_numberOfPassedCourses<\/code> in <code>johnSecond<\/code> and again test the <code>Equals<\/code> method and the <code>==<\/code> operator.<\/p>\n<p>Now, if we run the program, we can see the result:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">Both indicate the same person.\r\nBoth indicate the same person.\r\nBoth indicate the same person.\r\nBoth indicate the same person.\r\nThey indicate different persons.<\/pre>\n<h2><a id=\"Advantages\"><\/a>What Are the Advantages of Operator Overloading?<\/h2>\n<p>Operator overloading provides us with various advantages while coding. It makes the code easier and more understandable. Operator overloading offers support for user-defined types in the same way as built-in types do. We can use notation that is closely related to the target domain.<\/p>\n<h2><a id=\"Conclusion\"><\/a>Conclusion<\/h2>\n<p>In this article, we\u2019ve learned about operator overloading, the rules, syntax, and benefits. We\u2019ve also seen an example and implementation to understand it better.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we are going to learn about operator overloading in C#. It gives us various flexibility when we work with user-defined types. Let&#8217;s start. What Is Operator Overloading? Operator overloading is a technique to redefine a built-in operator. C#, with the help of operator overloading, allows us to use the same built-in operators [&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":[1067,1068,220,1069,1070],"class_list":["post-64604","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-csharp","tag-equals","tag-operator-overloading","tag-operators","tag-overloading-binary-operator","tag-overloading-unary-operator","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>Operator Overloading in C# - Code Maze<\/title>\n<meta name=\"description\" content=\"C# operator overloading, is all operator overloadable?, unary, binary operator overloading, real-life code example, benefits, advantages\" \/>\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-operator-overloading\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Operator Overloading in C# - Code Maze\" \/>\n<meta property=\"og:description\" content=\"C# operator overloading, is all operator overloadable?, unary, binary operator overloading, real-life code example, benefits, advantages\" \/>\n<meta property=\"og:url\" content=\"https:\/\/code-maze.com\/csharp-operator-overloading\/\" \/>\n<meta property=\"og:site_name\" content=\"Code Maze\" \/>\n<meta property=\"article:published_time\" content=\"2022-02-14T06:28:19+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-08T14:01:31+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=\"4 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-operator-overloading\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/csharp-operator-overloading\/\"},\"author\":{\"name\":\"Code Maze\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04\"},\"headline\":\"Operator Overloading in C#\",\"datePublished\":\"2022-02-14T06:28:19+00:00\",\"dateModified\":\"2023-11-08T14:01:31+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/code-maze.com\/csharp-operator-overloading\/\"},\"wordCount\":850,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/csharp-operator-overloading\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"keywords\":[\"Equals\",\"Operator Overloading\",\"Operators\",\"Overloading Binary Operator\",\"Overloading Unary Operator\"],\"articleSection\":[\"C#\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/code-maze.com\/csharp-operator-overloading\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/code-maze.com\/csharp-operator-overloading\/\",\"url\":\"https:\/\/code-maze.com\/csharp-operator-overloading\/\",\"name\":\"Operator Overloading in C# - Code Maze\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/code-maze.com\/csharp-operator-overloading\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/csharp-operator-overloading\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"datePublished\":\"2022-02-14T06:28:19+00:00\",\"dateModified\":\"2023-11-08T14:01:31+00:00\",\"description\":\"C# operator overloading, is all operator overloadable?, unary, binary operator overloading, real-life code example, benefits, advantages\",\"breadcrumb\":{\"@id\":\"https:\/\/code-maze.com\/csharp-operator-overloading\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/code-maze.com\/csharp-operator-overloading\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/csharp-operator-overloading\/#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-operator-overloading\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/code-maze.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Operator Overloading 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":"Operator Overloading in C# - Code Maze","description":"C# operator overloading, is all operator overloadable?, unary, binary operator overloading, real-life code example, benefits, advantages","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-operator-overloading\/","og_locale":"en_US","og_type":"article","og_title":"Operator Overloading in C# - Code Maze","og_description":"C# operator overloading, is all operator overloadable?, unary, binary operator overloading, real-life code example, benefits, advantages","og_url":"https:\/\/code-maze.com\/csharp-operator-overloading\/","og_site_name":"Code Maze","article_published_time":"2022-02-14T06:28:19+00:00","article_modified_time":"2023-11-08T14:01:31+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":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["Article","BlogPosting"],"@id":"https:\/\/code-maze.com\/csharp-operator-overloading\/#article","isPartOf":{"@id":"https:\/\/code-maze.com\/csharp-operator-overloading\/"},"author":{"name":"Code Maze","@id":"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04"},"headline":"Operator Overloading in C#","datePublished":"2022-02-14T06:28:19+00:00","dateModified":"2023-11-08T14:01:31+00:00","mainEntityOfPage":{"@id":"https:\/\/code-maze.com\/csharp-operator-overloading\/"},"wordCount":850,"commentCount":0,"publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"image":{"@id":"https:\/\/code-maze.com\/csharp-operator-overloading\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","keywords":["Equals","Operator Overloading","Operators","Overloading Binary Operator","Overloading Unary Operator"],"articleSection":["C#"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/code-maze.com\/csharp-operator-overloading\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/code-maze.com\/csharp-operator-overloading\/","url":"https:\/\/code-maze.com\/csharp-operator-overloading\/","name":"Operator Overloading in C# - Code Maze","isPartOf":{"@id":"https:\/\/code-maze.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/code-maze.com\/csharp-operator-overloading\/#primaryimage"},"image":{"@id":"https:\/\/code-maze.com\/csharp-operator-overloading\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","datePublished":"2022-02-14T06:28:19+00:00","dateModified":"2023-11-08T14:01:31+00:00","description":"C# operator overloading, is all operator overloadable?, unary, binary operator overloading, real-life code example, benefits, advantages","breadcrumb":{"@id":"https:\/\/code-maze.com\/csharp-operator-overloading\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/code-maze.com\/csharp-operator-overloading\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/csharp-operator-overloading\/#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-operator-overloading\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/code-maze.com\/"},{"@type":"ListItem","position":2,"name":"Operator Overloading 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\/64604","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=64604"}],"version-history":[{"count":4,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/64604\/revisions"}],"predecessor-version":[{"id":65952,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/64604\/revisions\/65952"}],"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=64604"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/categories?post=64604"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/tags?post=64604"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}