{"id":115,"date":"2020-06-07T02:20:54","date_gmt":"2020-06-07T02:20:54","guid":{"rendered":"https:\/\/turtleflyblog.com\/?p=115"},"modified":"2022-02-28T10:11:12","modified_gmt":"2022-02-28T10:11:12","slug":"delegate","status":"publish","type":"post","link":"https:\/\/turtleflyblog.com\/en\/2020\/06\/07\/delegate\/","title":{"rendered":"C# Delegation"},"content":{"rendered":"\n

C# delegation works like C\/C++ function pointer (type safe). We can use delegation as the parameter of a method and pass them between methods. This helps the programmer to follow one of SOLID principles \u2013 OCP. By using delegation, we don\u2019t have to write long and tedious if-else<\/strong> or switch<\/strong> statements. The example of using delegation is shown below:<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n
\"\"<\/figure>\n\n\n\n

  We create three methods which attempt to get the area from three different shapes (rectangle, circle and straight-triangle.<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

Shape is enum<\/strong> type and we use it as the parameter of the method GetShapeArea.<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

Do you notice that if we want to get more and more various shapes\u2019 areas, we then have to modify GetShapeArea <\/strong>method to add more case<\/strong>. This alternatively will increase the difficulty of the code\u2019s maintenance. Now we try to use delegation.<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

Do you see the difference? If we pass delegation as the parameter of the method, we then don\u2019t have to change the method\u2019s code. This makes the code simpler and thereby improve its productivity.<\/p>\n\n\n\n

On the other hand, we can add several same format delegation and be able to remove one of them. If we write the code like this:<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

We will get the result of the code execution<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

If you don\u2019t want to include the circle\u2019s one. You just have to write<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n
\"\"<\/figure>\n\n\n\n

Anonymous delegation<\/strong><\/p>\n\n\n\n

Delegation can also be anonymous.<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

After executing the above code, you will get the console like this<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

There are also other types of delegation (Action, Func, Predicate<\/strong>)<\/p>\n\n\n\n

The return type of Action<\/strong> is void. Action<int, char> means the parameters of Action is integer and char.<\/p>\n\n\n\n

Func<\/strong> has the return type. Func<int,string> means the return type of the function is string and this method only has one type parameter which is integer.<\/p>\n\n\n\n

Predicate <\/strong>means the return type of the method is always bool. Predicate<int> means the method has one integer parameter with the return type bool.<\/p>\n","protected":false},"excerpt":{"rendered":"

C# delegation works like C\/C++ function pointer (type safe). We can use delegation as the parameter of a method and pass them between methods. This helps the programmer to follow one of SOLID principles \u2013 OCP. By using delegation, we don\u2019t have to write long and tedious if-else or switch statements. The example of using […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"site-sidebar-layout":"default","site-content-layout":"default","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[33],"tags":[],"class_list":["post-115","post","type-post","status-publish","format-standard","hentry","category-programming-2"],"yoast_head":"\nC# Delegation - Who said turtle cannot fly<\/title>\n<meta name=\"description\" content=\"w one of SOLID principles \u2013 OCP. By using delegation, we don\u2019t have to write long and tedious if-else or switch statements. The example of using delegation is shown below:\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/turtleflyblog.com\/en\/2020\/06\/07\/delegate\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C# Delegation - Who said turtle cannot fly\" \/>\n<meta property=\"og:description\" content=\"w one of SOLID principles \u2013 OCP. By using delegation, we don\u2019t have to write long and tedious if-else or switch statements. The example of using delegation is shown below:\" \/>\n<meta property=\"og:url\" content=\"https:\/\/turtleflyblog.com\/en\/2020\/06\/07\/delegate\/\" \/>\n<meta property=\"og:site_name\" content=\"Who said turtle cannot fly\" \/>\n<meta property=\"article:published_time\" content=\"2020-06-07T02:20:54+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-02-28T10:11:12+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/turtleflyblog.com\/wp-content\/uploads\/2020\/10\/image.png\" \/>\n<meta name=\"author\" content=\"Joseph\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Joseph\" \/>\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\":\"WebPage\",\"@id\":\"https:\/\/turtleflyblog.com\/en\/2020\/06\/07\/delegate\/\",\"url\":\"https:\/\/turtleflyblog.com\/en\/2020\/06\/07\/delegate\/\",\"name\":\"C# Delegation - Who said turtle cannot fly\",\"isPartOf\":{\"@id\":\"https:\/\/turtleflyblog.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/turtleflyblog.com\/en\/2020\/06\/07\/delegate\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/turtleflyblog.com\/en\/2020\/06\/07\/delegate\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/turtleflyblog.com\/wp-content\/uploads\/2020\/10\/image.png\",\"datePublished\":\"2020-06-07T02:20:54+00:00\",\"dateModified\":\"2022-02-28T10:11:12+00:00\",\"author\":{\"@id\":\"https:\/\/turtleflyblog.com\/#\/schema\/person\/130c2e1bdce4a8ba27bfe14c65988172\"},\"description\":\"w one of SOLID principles \u2013 OCP. By using delegation, we don\u2019t have to write long and tedious if-else or switch statements. The example of using delegation is shown below:\",\"breadcrumb\":{\"@id\":\"https:\/\/turtleflyblog.com\/en\/2020\/06\/07\/delegate\/#breadcrumb\"},\"inLanguage\":\"en-AU\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/turtleflyblog.com\/en\/2020\/06\/07\/delegate\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-AU\",\"@id\":\"https:\/\/turtleflyblog.com\/en\/2020\/06\/07\/delegate\/#primaryimage\",\"url\":\"https:\/\/i0.wp.com\/turtleflyblog.com\/wp-content\/uploads\/2020\/10\/image.png?fit=543%2C392&ssl=1\",\"contentUrl\":\"https:\/\/i0.wp.com\/turtleflyblog.com\/wp-content\/uploads\/2020\/10\/image.png?fit=543%2C392&ssl=1\",\"width\":543,\"height\":392},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/turtleflyblog.com\/en\/2020\/06\/07\/delegate\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/turtleflyblog.com\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C# Delegation\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/turtleflyblog.com\/#website\",\"url\":\"https:\/\/turtleflyblog.com\/\",\"name\":\"Who said turtle cannot fly\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/turtleflyblog.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-AU\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/turtleflyblog.com\/#\/schema\/person\/130c2e1bdce4a8ba27bfe14c65988172\",\"name\":\"Joseph\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-AU\",\"@id\":\"https:\/\/turtleflyblog.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/539caa7bc4af75cd4ab82fc8d51935da?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/539caa7bc4af75cd4ab82fc8d51935da?s=96&d=mm&r=g\",\"caption\":\"Joseph\"},\"sameAs\":[\"https:\/\/turtleflyblog.com\/\/\"],\"url\":\"https:\/\/turtleflyblog.com\/author\/joseph\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C# Delegation - Who said turtle cannot fly","description":"w one of SOLID principles \u2013 OCP. By using delegation, we don\u2019t have to write long and tedious if-else or switch statements. The example of using delegation is shown below:","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:\/\/turtleflyblog.com\/en\/2020\/06\/07\/delegate\/","og_locale":"en_US","og_type":"article","og_title":"C# Delegation - Who said turtle cannot fly","og_description":"w one of SOLID principles \u2013 OCP. By using delegation, we don\u2019t have to write long and tedious if-else or switch statements. The example of using delegation is shown below:","og_url":"https:\/\/turtleflyblog.com\/en\/2020\/06\/07\/delegate\/","og_site_name":"Who said turtle cannot fly","article_published_time":"2020-06-07T02:20:54+00:00","article_modified_time":"2022-02-28T10:11:12+00:00","og_image":[{"url":"https:\/\/turtleflyblog.com\/wp-content\/uploads\/2020\/10\/image.png","type":"","width":"","height":""}],"author":"Joseph","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Joseph","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/turtleflyblog.com\/en\/2020\/06\/07\/delegate\/","url":"https:\/\/turtleflyblog.com\/en\/2020\/06\/07\/delegate\/","name":"C# Delegation - Who said turtle cannot fly","isPartOf":{"@id":"https:\/\/turtleflyblog.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/turtleflyblog.com\/en\/2020\/06\/07\/delegate\/#primaryimage"},"image":{"@id":"https:\/\/turtleflyblog.com\/en\/2020\/06\/07\/delegate\/#primaryimage"},"thumbnailUrl":"https:\/\/turtleflyblog.com\/wp-content\/uploads\/2020\/10\/image.png","datePublished":"2020-06-07T02:20:54+00:00","dateModified":"2022-02-28T10:11:12+00:00","author":{"@id":"https:\/\/turtleflyblog.com\/#\/schema\/person\/130c2e1bdce4a8ba27bfe14c65988172"},"description":"w one of SOLID principles \u2013 OCP. By using delegation, we don\u2019t have to write long and tedious if-else or switch statements. The example of using delegation is shown below:","breadcrumb":{"@id":"https:\/\/turtleflyblog.com\/en\/2020\/06\/07\/delegate\/#breadcrumb"},"inLanguage":"en-AU","potentialAction":[{"@type":"ReadAction","target":["https:\/\/turtleflyblog.com\/en\/2020\/06\/07\/delegate\/"]}]},{"@type":"ImageObject","inLanguage":"en-AU","@id":"https:\/\/turtleflyblog.com\/en\/2020\/06\/07\/delegate\/#primaryimage","url":"https:\/\/i0.wp.com\/turtleflyblog.com\/wp-content\/uploads\/2020\/10\/image.png?fit=543%2C392&ssl=1","contentUrl":"https:\/\/i0.wp.com\/turtleflyblog.com\/wp-content\/uploads\/2020\/10\/image.png?fit=543%2C392&ssl=1","width":543,"height":392},{"@type":"BreadcrumbList","@id":"https:\/\/turtleflyblog.com\/en\/2020\/06\/07\/delegate\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/turtleflyblog.com\/en\/"},{"@type":"ListItem","position":2,"name":"C# Delegation"}]},{"@type":"WebSite","@id":"https:\/\/turtleflyblog.com\/#website","url":"https:\/\/turtleflyblog.com\/","name":"Who said turtle cannot fly","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/turtleflyblog.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-AU"},{"@type":"Person","@id":"https:\/\/turtleflyblog.com\/#\/schema\/person\/130c2e1bdce4a8ba27bfe14c65988172","name":"Joseph","image":{"@type":"ImageObject","inLanguage":"en-AU","@id":"https:\/\/turtleflyblog.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/539caa7bc4af75cd4ab82fc8d51935da?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/539caa7bc4af75cd4ab82fc8d51935da?s=96&d=mm&r=g","caption":"Joseph"},"sameAs":["https:\/\/turtleflyblog.com\/\/"],"url":"https:\/\/turtleflyblog.com\/author\/joseph\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/turtleflyblog.com\/wp-json\/wp\/v2\/posts\/115","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/turtleflyblog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/turtleflyblog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/turtleflyblog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/turtleflyblog.com\/wp-json\/wp\/v2\/comments?post=115"}],"version-history":[{"count":1,"href":"https:\/\/turtleflyblog.com\/wp-json\/wp\/v2\/posts\/115\/revisions"}],"predecessor-version":[{"id":677,"href":"https:\/\/turtleflyblog.com\/wp-json\/wp\/v2\/posts\/115\/revisions\/677"}],"wp:attachment":[{"href":"https:\/\/turtleflyblog.com\/wp-json\/wp\/v2\/media?parent=115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/turtleflyblog.com\/wp-json\/wp\/v2\/categories?post=115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/turtleflyblog.com\/wp-json\/wp\/v2\/tags?post=115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}