{"id":381,"date":"2023-08-03T06:07:00","date_gmt":"2023-08-03T06:07:00","guid":{"rendered":"http:\/\/192.168.0.142\/?p=381"},"modified":"2023-08-03T06:16:56","modified_gmt":"2023-08-03T06:16:56","slug":"delegates-in-c","status":"publish","type":"post","link":"http:\/\/192.168.0.142\/delegates-in-c\/","title":{"rendered":"Delegates in C#"},"content":{"rendered":"\n
There are three types of delegates that can be used in C#.<\/p>\n\n\n\n
Single delegate can be used to invoke a single method.<\/p>\n\n\n\n
\/\/ Declare a delegate\ndelegate double CalculateSimpleInterest(double p, double t, double r);\nstatic CalculateSimpleInterest SI = getTotalInterest;\n\nstatic double getTotalInterest(double p, double t, double r)\n{\nreturn (p * t * r) \/ 100;\n}\n\ndouble totalInterest;\n\/\/Method I : Invocation of simple delegate by using Invoke keyword\ntotalInterest = SI.Invoke(120, 1, 3.25);\n\n\/\/Method II : Invocation of simple delegate by passing method name CalculateSimpleInterest D = new CalculateSimpleInterest(getTotalInterest);\ntotalInterest = D(120, 1, 3.25);\n<\/code><\/pre>\n\n\n\nMulticast Delegate<\/h4>\n\n\n\n
can be used to invoke multiple methods. The delegate instance can do multicasting (adding new method on existing delegate instance) using + operator and \u2013 operator can be used to remove a method from a delegate instance. All methods will invoke in sequence as they are assigned.<\/p>\n\n\n\n
\/\/ Declare a delegate\ndelegate double CalculateSimpleInterest(double para1, double para2, double para3);\nstatic CalculateSimpleInterest dObjSI = getTotalInterest;\n\nstatic double getTotalInterest(double p, double t, double r)\n{\nreturn (p * t * r) \/ 100;\n}\nstatic double getInterestRatePerYear(double SI, double p, double t)\n{\n return (SI * 100) \/ (p * t);\n}\nstatic double getInterestTimeSpan(double SI, double p, double r)\n{\n return (SI * 100) \/ (p * r);\n}\n\ndouble SI;\n\/\/Calculating simple interest\nSI = dObjSI.Invoke(120, 1, 3.25);\n\/\/using multicast delegate by invoking method getInterestRatePerYear()\ndObjSI += new CalculateSimpleInterest(getInterestRatePerYear);\ndouble Rate = dObjSI.Invoke(SI, 120, 1);\n\n\/\/using multicast delegate by invoking method getInterestTimeSpan()\ndObjSI += new CalculateSimpleInterest(getInterestTimeSpan);\ndouble TimeSpan = dObjSI.Invoke(SI, 120, 3.25);\n<\/code><\/pre>\n\n\n\nGeneric Delegate<\/h4>\n\n\n\n\n- don\u2019t require to define the delegate instance in order to invoke the methods. There are three types of generic delegates.<\/li>\n\n\n\n
- was introduced in .Net 3.5<\/li>\n<\/ul>\n\n\n\n
1) Func
2) Action
3) Predicate<\/p>\n\n\n\n
Action Delegate Type<\/h5>\n\n\n\n\n- The generic Action delegate represents a method that returns void. Different versions of Action take between 0 and 18 input parameters.<\/li>\n\n\n\n
- The action delegate that takes two arguments.<\/li>\n\n\n\n
- public delegate void Action<in T1, in T2>(T1 arg1, T2 arg2)<\/li>\n<\/ul>\n\n\n\n
Func Delegate Type<\/h5>\n\n\n\n\n- Func in short is parameterized delegate.<\/li>\n\n\n\n
- The generic Func delegate represents a method that returns a value.<\/li>\n\n\n\n
- Func handles many arguments.<\/li>\n\n\n\n
- It provides a way to store anonymous methods in a generalized and simple way.<\/li>\n<\/ul>\n\n\n\n
\/\/ Create a Func instance that has one parameter and one return value.\n\/\/ ... Parameter is an integer, result value is a string.\n Func<int, string> func1 = (x) => string.Format(\"string = {0}\", x);\n\n\/\/ Func instance with two parameters and one result.\n\/\/ ... Receives bool and int, returns string.\n Func<bool, int, string> func2 = (b, x) =>\n string.Format(\"string = {0} and {1}\", b, x);\n \n\/\/ Func instance that has no parameters and one result value.\n Func<double> func3 = () => Math.PI \/ 2;\n\n\/\/ Call the Invoke instance method on the anonymous functions.\n Console.WriteLine(func1.Invoke(5));\n Console.WriteLine(func2.Invoke(true, 10));\n Console.WriteLine(func3.Invoke());<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"There are three types of delegates that can be used in C#. Single Delegate Single delegate can be used to invoke a single method. Multicast Delegate can be used to invoke multiple methods. The delegate instance can do multicasting (adding new method on existing delegate instance) using + operator and \u2013 operator can be used … Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[33,37],"_links":{"self":[{"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/posts\/381"}],"collection":[{"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/comments?post=381"}],"version-history":[{"count":8,"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/posts\/381\/revisions"}],"predecessor-version":[{"id":394,"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/posts\/381\/revisions\/394"}],"wp:attachment":[{"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/media?parent=381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/categories?post=381"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/192.168.0.142\/wp-json\/wp\/v2\/tags?post=381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}