In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "the basic concepts and characteristics of C# delegation". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn the basic concepts and characteristics of C# delegation.
Delegation has the following characteristics:
A delegate is similar to a C++ function pointer, but it is type safe.
Delegates allow methods to be passed as parameters.
Delegates can be used to define callback methods.
Delegates can be linked together; for example, multiple methods can be called on an event.
Method does not need to match exactly with the delegate signature. For more information, see Covariant and Inverter.
Version 2.0 of C # introduces the concept of anonymous methods, which allow code blocks to be passed as parameters instead of individually defined methods.
There are three steps to using a delegate in C#:
1. Define a delegate:
/ / declare delegate public delegate void MyDel ()
two。 Instantiate the delegate:
TestDel t = new TestDel (); Console.WriteLine ("- the following is a simple demonstration of using a delegate -"); / / t.MyMethod (); / / instantiate the delegate with a method / the method signature should be consistent with the delegate signature MyDel del = new MyDel (t.MyMethod)
3. Invoke the delegate:
/ / call delegate del ()
Well, in fact, the change of delegation is very complicated, but basically all of them will conform to these three steps. Having said that, let's take a look at the complete code:
Namespace DelegateDemo {/ / declare delegate public delegate void MyDel (); / / declare delegate public delegate void MyDel2 with parameters (int num1, int num2); / / declare delegate public delegate string MyDel3 with return value (string s); / / declare delegate to demonstrate anonymous method public delegate string ProcessString (string s); class Program {static void Main (string [] args) {# region delegate presentation / * TestDel t = new TestDel () # region simply instantiate delegate and invoke delegate Console.WriteLine ("- the following is a simple demonstration of using delegate -"); / / t.MyMethod (); / / instantiate delegate with a method / the method signature should be consistent with delegate signature MyDel del = new MyDel (t.MyMethod); / / call delegate del () After / / C # 2.0, you can instantiate the delegate MyDel del4 = t.MyMethod; del4 (); / / instantiate del4 = TestDel.MyStaticMethod; del4 () by static method; / / the following code has the same effect / / MyDel2 del2 = new MyDel2 (t.MyMethod); / / del2 (10,20); MyDel2 del2 = t.MyMethod; del2 (10,20); / / MyDel3 del3 = new MyDel3 (t.MyMethod) / / Console.WriteLine (del3 ("abc")); # endregion # region anonymous method instantiation delegate Console.WriteLine ("- the following is an anonymous method demonstration -"); / / instantiate delegate p = delegate (string inputString) {return inputString.ToUpper ();}; / / invoke anonymous method Console.WriteLine (p ("aaaa") through delegate) # endregion # region delegated Multicast demonstration Console.WriteLine ("- the following is delegated Multicast demonstration -"); MyDel mydel1 = t.MyMethod2; MyDel mydel3 = TestDel.MyMethod3; MyDel allMyDel = mydel1 + mydel2 + mydel3; allMyDel (); allMyDel-= mydel3; allMyDel () # endregion # region delegate demonstrates Console.WriteLine as a parameter ("- the following is demonstrated as a parameter -"); MyDel3 paramMyDel3 = t.MyMethod; TestDel.MyParamMethod ("aaa", paramMyDel3); # endregion # region delegate as a return value Console.WriteLine ("--following is a demonstration of a delegate as a return value -") / / the return value of returnMyDel pointing to t.MyReturnMethod () MyDel3 returnMyDel = t.MyReturnMethod (); / / returnMyDel pointing to t.MyMethod / / MyDel3 returnMyDel = t.MyMethod; Console.WriteLine (returnMyDel ("sssssssssssss")); # endregion * / # endregion / / MyReturnDelegateTest my = new MyReturnDelegateTest (); / / my.MyTest (); MyParamDelegateTest myParam = new MyParamDelegateTest (); myParam.AddBooks (); myParam.MyTest () } public class TestDel {# region Common method public static void MyStaticMethod () {Console.WriteLine ("MyStaticMethod");} public void MyMethod () {Console.WriteLine ("MyMethod");} public void MyMethod2 () {Console.WriteLine ("MyMethod 22222222222");} public static void MyMethod3 () {Console.WriteLine ("MyMethod 3333333333333") } public void MyMethod (int num1, int num2) {Console.WriteLine (num1+num2);} public string MyMethod (string s) {return s.ToUpper ();} # endregion / delegate as method parameter / public static void MyParamMethod (string s, MyDel3 del3) {Console.WriteLine (del3 (s)) } / delegate as the return value / public MyDel3 MyReturnMethod () {/ return the method return MyMethod; that conforms to the delegate specification}}
Through an example, we can know that multiple methods can be bound to the same delegate variable using a delegate, and when this variable is called (the word "call" is used here because the variable represents a method), all bound methods can be called in turn.
At this point, I believe you have a deeper understanding of "the basic concepts and characteristics of C# delegation". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.