{"id":326,"date":"2024-08-07T23:13:34","date_gmt":"2024-08-07T17:13:34","guid":{"rendered":"https:\/\/www.texeltech.com\/?p=326"},"modified":"2024-08-07T23:38:55","modified_gmt":"2024-08-07T17:38:55","slug":"c-delegates","status":"publish","type":"post","link":"http:\/\/www.texeltech.com\/?p=326","title":{"rendered":"C# Delegates"},"content":{"rendered":"\n<p><strong>Delegates<\/strong> refers to an <strong>Method <\/strong>pointer. What does it mean? Let us explain object and class. By creating an Object we refer to a class, example as follows:<\/p>\n\n\n\n<p>Suppose we have a <strong>Product Class<\/strong>. Now we have to create a <strong>Product<\/strong> class <strong>object<\/strong> as following:<\/p>\n\n\n\n<p>Product <strong>prod<\/strong> = new <strong>Product()<\/strong><\/p>\n\n\n\n<p>As like that now well create a Delegates:<\/p>\n\n\n\n<p>1st we will create a Method Add<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>static int <strong>Add<\/strong>(int firstNumber, int secondNumber){\nreturn firstNumber + secondNumber;\n}\n\nint result = <strong>Add<\/strong>(2,4); \/\/ This is a common scenario.\n\n<strong>We can write the above part as follows<\/strong>. <strong>But, to write like this we need to do one thing first<\/strong>**\n<em>AddHandler<\/em> addHandle = <strong>Add<\/strong>; \n\n** delegate int <em>AdHandler<\/em> (int firstNumber, int secondNumber);\n<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>So, now we can write all things as follows:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>delegate int <em><strong>AdHandler<\/strong><\/em> (int firstNumber, int secondNumber);\n\nstatic int <strong>Add<\/strong>(int firstNumber, int secondNumber){\nreturn firstNumber + secondNumber;\n}\n\n<em><strong>AdHandler<\/strong><\/em> <strong>adHandle<\/strong> = Add;\n\nint result = <strong>adHandle<\/strong>(2, 4)\n\nconsole.writeline(result) \/\/ 6 <\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Delegates refers to an Method pointer. What does it mean? Let us explain object and class. By creating an Object we refer to a class, example as follows: Suppose we have a Product Class. Now&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38,19],"tags":[],"_links":{"self":[{"href":"http:\/\/www.texeltech.com\/index.php?rest_route=\/wp\/v2\/posts\/326"}],"collection":[{"href":"http:\/\/www.texeltech.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.texeltech.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.texeltech.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.texeltech.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=326"}],"version-history":[{"count":3,"href":"http:\/\/www.texeltech.com\/index.php?rest_route=\/wp\/v2\/posts\/326\/revisions"}],"predecessor-version":[{"id":329,"href":"http:\/\/www.texeltech.com\/index.php?rest_route=\/wp\/v2\/posts\/326\/revisions\/329"}],"wp:attachment":[{"href":"http:\/\/www.texeltech.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=326"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.texeltech.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=326"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.texeltech.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=326"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}