In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
The knowledge points of this article "Multicast delegation and generic delegation case Analysis in C#" are not quite understood by most people, so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Multicast delegation and generic delegation example Analysis in C#" article.
A brief introduction to Multicast delegation
Each delegate is inherited from MulticastDelegate, that is, each is a multicast delegate.
A multicast delegate with a return value returns only the value of the last method
Multicast delegates can use plus or minus signs to increase or decrease methods.
The delegate instance generated when the same method is passed to the delegate is also the same (that is, the same delegate)
Code implementation / / declare delegate delegate void MulticastTest (); public class MulticastDelegateTest {public void Show () {MulticastTest multicastTest = new MulticastTest (MethodTest); multicastTest (); Action action = new Action (MethodTest); action = (Action) MulticastDelegate.Combine (action, new Action (MethodTest2)) Action = (Action) MulticastDelegate.Combine (action, new Action (MethodTest3)); action = (Action) MulticastDelegate.Remove (action, new Action (MethodTest3)); action (); / / equivalent to the above action = MethodTest; action + = MethodTest2; action + = MethodTest3; action-= MethodTest3 Foreach (Action action1 in action.GetInvocationList ()) {action1 ();} Console.WriteLine ("="); action (); Func func = () = > {return "I am Lambda";}; func + = () = > {return "I am func1";} Func + = () = > {return "I am func2";}; func + = GetTest; func + = GetTest; / / the delegate instance generated when passing the same method to the delegate is the same (that is, the same delegate) string result = func (); Console.WriteLine (result); Console.WriteLine ("=") } # region delegate method public void MethodTest () {Console.WriteLine ("I am method MethodTest () 1");} public void MethodTest2 () {Console.WriteLine ("I am method MethodTest () 2") } public void MethodTest3 () {Console.WriteLine ("I am method MethodTest () 3");} public string GetTest () {return "I am method GetTest ()";} # endregion} generic delegate code implementation / / generic delegate declaration delegate void GenericDelegate (T t) Public class GenericDelegate {public static void InvokeDelegate () {GenericDelegate genericDelegate = new GenericDelegate (Method1); genericDelegate ("I am generic delegate 1"); / / official version (no return value) Action action = new Action (Method1); action ("I am generic delegate 1") / / Action GenericDelegate genericDelegate1 = new GenericDelegate (Method2); genericDelegate1 (2); / / official version (return value) Func func = new Func (Method3); string ret = func ("I am a Func delegate with return value"); Console.WriteLine (ret) / / Func} # region delegate method public static void Method1 (string str) {Console.WriteLine (str);} public static void Method2 (int num) {Console.WriteLine ("I am generic delegate 2" + num);} public static string Method3 (string str) {return str } # endregion} the above article is about "Multicast delegation and generic delegation instance Analysis in C#". I believe everyone has a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to learn more about related knowledge, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.