{"id":179,"date":"2009-07-24T22:37:31","date_gmt":"2009-07-24T21:37:31","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=179"},"modified":"2009-07-24T22:37:31","modified_gmt":"2009-07-24T21:37:31","slug":"operator","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2009\/07\/24\/operator\/","title":{"rendered":"Operator"},"content":{"rendered":"<p>The standard class function are not able to utilize the standard operators within the c# language, for example the multiple, add, subtraction and divide in a mathematics example.<\/p>\n<p>To overcome this problem, there is a &#8216;operator&#8217; special syntax that allows for this to take place.  The operator can work with any of the c# language standard functions of which you are able to program there functional aspects within the class.  The syntax for this is<\/p>\n<pre lang=\"csharp\">\r\npublic static <return type> operator <operator type>(<parameter list of passed variables>);\r\n<\/pre>\n<p>for example if there was an return type of int and two integer values passed in the parameter list and using the addition operator.<\/p>\n<pre lang=\"csharp\">\r\npublic static int operator +(int a, int b)\r\n<\/pre>\n<p>Below is some code that will demonstrate this further within a general code development.<\/p>\n<pre lang=\"csharp\">\r\nusing System;\r\n\r\nclass operatorBase\r\n{\r\n       private int i;       \/\/ private member of the class\r\n       \r\n       public operatorBase() \r\n       {\r\n              i = 0;       \r\n       }\r\n\r\n       public operatorBase(int init)  \r\n       {\r\n              this.i = init; \r\n       }\r\n\r\n       \/\/ get and set the value for the private member i\r\n       public int Value\r\n       {\r\n              get { return i;}\r\n              set { i = value;}\r\n       }\r\n\r\n       \/\/ the operator +, parameters are the two values that you want to add, can be overloaded with different values\r\n       \/\/ e.g. (int i2, int i3) for example.\r\n       public static operatorBase operator +(operatorBase i2, operatorBase i3)\r\n       {\r\n              \/\/ create the return;\r\n              operatorBase locali= new operatorBase();\r\n              locali.i = i2.i + i3.i;  have access to the internals of passed parameters\r\n              return  locali;       \/\/ return the operatorBase class\r\n       }\r\n}\r\n\r\nclass operatorTest\r\n{\r\n\r\n       public static void Main()\r\n       {\r\n              operatorBase opBase = new operatorBase();\r\n\r\n              \/\/ set the value to 3 and also output the value;\r\n              opBase.Value = 3;\r\n              Console.WriteLine(opBase.Value);\r\n\r\n              operatorBase opBase2 = new operatorBase(4);\r\n\r\n              \/\/ to add to the operatorbases together, but will return an operatorBase, thus bracket the equation and use the .Value to get the value. \r\n              Console.WriteLine((opBase + opBase2).Value);\r\n\r\n              \/\/ since creating two new on the fly operatorBase, then the result is an int value again.\r\n              Console.WriteLine((new operatorBase().Value = 3) + (new operatorBase().Value = 2));\r\n       }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The standard class function are not able to utilize the standard operators within the c# language, for example the multiple, add, subtraction and divide in a mathematics example. To overcome this problem, there is a &#8216;operator&#8217; special syntax that allows for this to take place. The operator can work with any of the c# language &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2009\/07\/24\/operator\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Operator<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[14,398,13,400],"class_list":["post-179","post","type-post","status-publish","format-standard","hentry","category-c_sharp","tag-c-sharp","tag-linux","tag-mono","tag-windows"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/179","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=179"}],"version-history":[{"count":1,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/179\/revisions"}],"predecessor-version":[{"id":180,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/179\/revisions\/180"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=179"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=179"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=179"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}