{"id":533,"date":"2010-01-18T15:33:11","date_gmt":"2010-01-18T15:33:11","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=533"},"modified":"2010-01-18T15:33:11","modified_gmt":"2010-01-18T15:33:11","slug":"delegates-csharp","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/01\/18\/delegates-csharp\/","title":{"rendered":"Delegates &#8211; csharp"},"content":{"rendered":"<p>A delegate, is how to reference a method, something like <a href=\"http:\/\/en.wikipedia.org\/wiki\/Function_pointer\">function pointer in c++<\/a>.  One of the main questions of why is there a delegate\/function pointer, it is because it gives the developer\/user maximum amount of flexibility, lets say that you have a list of functions that you want to call if someone passes you there name and these functions could be, join together with surname, add in there ID code to there class and some other functions at the users decision like what is there car registration etc. well a delegate\/function pointer can allow for different methods be called without having a set process at compile time.  You could take path a , b or even c.  It is harder to test with sometimes, but as long as the test process has a line to follow as well then things are good.<\/p>\n<p>The delegate syntax is like other methods apart from the operative word delegate is just before the return type and there is no code within the body, it is more of a skeleton of a method\/function and return type.<\/p>\n<p>Here is the basic delegate syntax.<\/p>\n<pre lang=\"csharp\">\r\npublic delegate int Compare(object object1, object object2);\r\n<\/pre>\n<p>I have included this within the code below, what the code does is to create 5 names, and there is a sort to order the names with the returned value from the compared method\/function (bubble sort).  But the Compare function could be changed to order A-Z, Z-A etc.. since you can have different Compare&#8217;s methods coded but just alter one line of code to call the new method\/function.<\/p>\n<pre lang=\"csharp\">\r\nCompare compare = new Compare(SimpleDelegateName.CompareNames);\r\n<\/pre>\n<p>that one.<\/p>\n<p>Anyway, hope that the code\/comments make sense and also help to give reasons for the delegate and how it is useful. <\/p>\n<pre lang=\"csharp\">\r\nusing System;\r\n\r\nnamespace SimpleDelegate\r\n{\r\n\t\/\/ setup a basic delegate to compare two objects.\r\n\t\/\/ return values = 0 (same), 1 (object1 > object2), -1 (object1 < object2)\r\n\tpublic delegate int Compare(object object1, object object2);\r\n\t\r\n\tclass SimpleDelegateName \r\n\t{\r\n\t\t\/\/ has to be a static otherwise when you try to create a instance of Compare (the delegate)\r\n\t\t\/\/ it will not have anywhere to point to, since static means create method even if not creating\r\n\t\t\/\/ the object\r\n\t\tpublic static int CompareNames(object obj1, object obj2)\r\n\t\t{\r\n\t\t\t\/\/ conver the objects into strings\r\n\t\t\tstring name1 = obj1.ToString();\r\n\t\t\tstring name2 = obj2.ToString();\r\n\t\t\t\r\n\t\t\tif (String.Compare(name2,name1) > 0)\r\n\t\t\t{\r\n\t\t\t\treturn 1;\r\n\t\t\t} else if (String.Compare(name2, name1) < 0)\r\n\t\t\t{\r\n\t\t\t\treturn -1;\r\n\t\t\t} else \r\n\t\t\t{\r\n\t\t\t\treturn 0;\r\n\t\t\t}\r\n\t\t}\r\n\t\t\r\n\t\tpublic SimpleDelegateName() \r\n\t\t{\r\n\t\t\tname = \"\";\r\n\t\t}\r\n\t\t\r\n\t\tpublic SimpleDelegateName(String pName)\r\n\t\t{\r\n\t\t\tname = pName;\r\n\t\t}\r\n\t\t\r\n\t\tpublic String returnName()\r\n\t\t{\r\n\t\t\treturn name;\r\n\t\t}\r\n\t\t\r\n\t\tprivate String name;\r\n\t}\r\n\t\r\n\tclass MainClass\r\n\t{\r\n\t\tpublic static void Main(string[] args)\r\n\t\t{\r\n\t\t\t\/\/ create 5 names\r\n\t\t\tSimpleDelegateName[] names = new SimpleDelegateName[5];\r\n\t\t\tnames[0] = new SimpleDelegateName(\"Ian\");\r\n\t\t\tnames[1] = new SimpleDelegateName(\"John\");\r\n\t\t\tnames[2] = new SimpleDelegateName(\"Alice\");\r\n\t\t\tnames[3] = new SimpleDelegateName(\"Tom\");\r\n\t\t\tnames[4] = new SimpleDelegateName(\"Katie\");\r\n\t\t\t\/\/ they are no pictular order.\r\n\t\t\t\r\n\t\t\t\/\/ the delegate instance varibale\r\n\t\t\t\/\/ create a new compare object with the CompareNames method within the SimpleDeleteName class\r\n\t\t\tCompare compare = new Compare(SimpleDelegateName.CompareNames);\r\n\t\t\t\r\n\t\t\t\/\/ the basic bubble sort, moves 1 word at a time up\/down the array\r\n\t\t\tobject temp;\r\n\t\t\tfor (int i = 0; i < names.Length; i++)\r\n\t\t\t{\r\n\t\t\t\tfor (int j = 0; j < names.Length; j++)\r\n\t\t\t\t{\r\n\t\t\t\t\tConsole.WriteLine(\"DEBUG : i = \" + i + \" j = \" + j \r\n\t\t\t\t\t                   + \" name 1 =  \" + names[i].returnName() + \" name 2 = \" + names[j].returnName() \r\n\t\t\t\t\t                  \t+ \" compare \" + compare(names[i].returnName(), names[j].returnName()));\r\n\t\t\t\t\t\/\/ if the names[i] is greater than names[j] then swap the names around.\r\n\t\t\t\t\tif (compare(names[i].returnName(), names[j].returnName()) >0)\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tConsole.WriteLine(\"DEBUG : \" +names[i].returnName() + \"   \" + names[j].returnName());\r\n\t\t\t\t\t\ttemp = names[i];\r\n\t\t\t\t\t\tnames[i] = names[j];\r\n\t\t\t\t\t\tnames[j] = (SimpleDelegateName)temp;\r\n\t\t\t\t\t\t\/\/ swapped..\r\n\t\t\t\t\t\tConsole.WriteLine(\"DEBUG : \" +names[i].returnName() + \"   \" + names[j].returnName() + \" SWAPPED\");\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\t\/\/ print out the names in alphabetical order\r\n\t\t\tforeach (SimpleDelegateName n in names)\r\n\t\t\t{\r\n\t\t\t\tConsole.WriteLine(\"Name : \" + n.returnName());\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<pre lang=\"bash\">\r\nDEBUG : i = 0 j = 0 name 1 =  Ian name 2 = Ian compare 0\r\nDEBUG : i = 0 j = 1 name 1 =  Ian name 2 = John compare 1\r\nDEBUG : Ian   John\r\nDEBUG : John   Ian SWAPPED\r\nDEBUG : i = 0 j = 2 name 1 =  John name 2 = Alice compare -1\r\nDEBUG : i = 0 j = 3 name 1 =  John name 2 = Tom compare 1\r\nDEBUG : John   Tom\r\nDEBUG : Tom   John SWAPPED\r\nDEBUG : i = 0 j = 4 name 1 =  Tom name 2 = Katie compare -1\r\nDEBUG : i = 1 j = 0 name 1 =  Ian name 2 = Tom compare 1\r\nDEBUG : Ian   Tom\r\nDEBUG : Tom   Ian SWAPPED\r\nDEBUG : i = 1 j = 1 name 1 =  Tom name 2 = Tom compare 0\r\nDEBUG : i = 1 j = 2 name 1 =  Tom name 2 = Alice compare -1\r\nDEBUG : i = 1 j = 3 name 1 =  Tom name 2 = John compare -1\r\nDEBUG : i = 1 j = 4 name 1 =  Tom name 2 = Katie compare -1\r\nDEBUG : i = 2 j = 0 name 1 =  Alice name 2 = Ian compare 1\r\nDEBUG : Alice   Ian\r\nDEBUG : Ian   Alice SWAPPED\r\nDEBUG : i = 2 j = 1 name 1 =  Ian name 2 = Tom compare 1\r\nDEBUG : Ian   Tom\r\nDEBUG : Tom   Ian SWAPPED\r\nDEBUG : i = 2 j = 2 name 1 =  Tom name 2 = Tom compare 0\r\nDEBUG : i = 2 j = 3 name 1 =  Tom name 2 = John compare -1\r\nDEBUG : i = 2 j = 4 name 1 =  Tom name 2 = Katie compare -1\r\nDEBUG : i = 3 j = 0 name 1 =  John name 2 = Alice compare -1\r\nDEBUG : i = 3 j = 1 name 1 =  John name 2 = Ian compare -1\r\nDEBUG : i = 3 j = 2 name 1 =  John name 2 = Tom compare 1\r\nDEBUG : John   Tom\r\nDEBUG : Tom   John SWAPPED\r\nDEBUG : i = 3 j = 3 name 1 =  Tom name 2 = Tom compare 0\r\nDEBUG : i = 3 j = 4 name 1 =  Tom name 2 = Katie compare -1\r\nDEBUG : i = 4 j = 0 name 1 =  Katie name 2 = Alice compare -1\r\nDEBUG : i = 4 j = 1 name 1 =  Katie name 2 = Ian compare -1\r\nDEBUG : i = 4 j = 2 name 1 =  Katie name 2 = John compare -1\r\nDEBUG : i = 4 j = 3 name 1 =  Katie name 2 = Tom compare 1\r\nDEBUG : Katie   Tom\r\nDEBUG : Tom   Katie SWAPPED\r\nDEBUG : i = 4 j = 4 name 1 =  Tom name 2 = Tom compare 0\r\nName : Alice\r\nName : Ian\r\nName : John\r\nName : Katie\r\nName : Tom\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A delegate, is how to reference a method, something like function pointer in c++. One of the main questions of why is there a delegate\/function pointer, it is because it gives the developer\/user maximum amount of flexibility, lets say that you have a list of functions that you want to call if someone passes you &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/01\/18\/delegates-csharp\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Delegates &#8211; csharp<\/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":[],"class_list":["post-533","post","type-post","status-publish","format-standard","hentry","category-c_sharp"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/533","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=533"}],"version-history":[{"count":2,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/533\/revisions"}],"predecessor-version":[{"id":535,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/533\/revisions\/535"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=533"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=533"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}