Windows Mobile Development (ii)--retreat cultivation

Source: Internet
Author: User

Some of the great men in martial arts novels, in order to fight for the martial arts leader, call the world, often retreat to practice for a period of time, retreat can only contact the people who send rice, and the customs also has a lot of guards. Also, whether they are basketball or football players, they have to do a long period of physical exercise, such as running, before they really touch the ball.
If we want to become the martial arts of the procedural world, then we must also retreat to practice, and carefully cultivate basic skills.
So what are the basic skills of our developers of Windows Mobile development? In fact, there are many basic skills, the most important is the ability to think logically and solve problems, then say back, logical thinking and problem-solving ability from where? Doing and practicing is the best thing that can be embodied in the procedural world.
What do we do with the "Dick Silk" of our Windows Mobile development? I am using C # for Windows Mobile development, so I use the weapon is C #, all the tricks are also C # out, in the retreat this link, I mainly practice delegation, inheritance, polymorphism, garbage collection and generics, this time, I mainly exercise in C # delegate.
1.1 What is a delegatePeople who have studied C and C + + know pointers, and when we want to pass the method as a parameter, we can pass a pointer to the function, which is the memory address of the function, which causes the type to be unsafe and difficult to control, in C #, in order to avoid this problem, the delegate is a special type of object, It and the class should be level, that is, the delegate can be declared within a namespace, or it can be declared inside a class, and the delegate is also a memory address, but it is type-safe.Declaration of the 1.1.1 DelegateDelegates, like classes, need to be declared in advance, declared in the format:delegate int Dgetresult (int a,int b);1.1.2 Declaration of a delegate instanceAfter declaring the good one delegate, we need to declare a method instance, the method instance must be the same as the delegate, that is, the method return value type and the delegate are consistent, the type, number, order, and the delegate of the method parameters are consistent. An instance method of the above delegate:private int Getaddresult (int a,int b){return a+b;}Below, we can call this delegate in the main function, call the delegate and the class, also must request a delegate object, through the object call delegate, as follows:Dgetresult getresult = Getaddresult; You do not need to add arguments and parentheses here to point the address of the delegate object to the methodConsole.WriteLine (GetResult (2,4).   ToString ()); Execute by Delegate ObjectConsole.readkey ();1.1.3 Assignment of Delegate objects, addition and subtraction of methodsA delegate object can call multiple methods at the same time, we can add multiple instance methods to a delegate object, as follows:int Getminusresult (int a, int b)//new instance method{return a-B;}Call the method in the main function:GetResult +=getminusresult;Console.WriteLine (GetResult (2,4). ToString ());The running result is-2, if we add a breakpoint execution inside the method Getaddresult, we will find that the method Getaddresult is also executed.Since the delegate object can add methods, the delegate object can also prune the method.Gerresult-=getminusresult;Console.WriteLine (GetResult (2,4). ToString ());This time the operation of the program results is 6.1.1.4 Multicast delegation method blocked problemIn fact, the delegate we use above is called multicast delegation, there is a problem, when the multicast delegate method group in the previous method of the exception, then the delegate object method iteration will stop, the subsequent method will not be executed, how to deal with this problem?Look at the following code:Delegate[] Del=getresult.getinvocationlist ();foreach (Dgetresult _getresult in Del){Try{_getresult (4,2);}catch (Exception){Console.WriteLine ("Exception was thrown");}}This method is that the delegate object returns an array of delegate objects through the Getinvocationlist () method, and then iterates through the delegate array for custom processing, so that the previous method has an exception, and the delegate object iterates over the method. The latter method will not be blocked by the previous exception.1.2 Why should I have a delegateFirst, the delegate is type-safe, the delegate is easy to control, and the efficiency is high; In addition, the use of delegates in large applications can reduce dependency and layer coupling, providing support for developing higher performance components.1.3 Lambda expressionThere are anonymous methods in C #, but the anonymous method is not very convenient to write, so c#3.0 started, there is a lambda expression, this syntax can only use super cool two words to get used to, this is the C # to his programmer to open the comparisonof, C # Programmers must be happy for it.Lambda expressions consist of: (parameter 1, parameter 2 ...) =>{Concrete Implementation};Every place with a delegate parameter type can use a lambda expression, and an anonymous method should be a very cool thing to write with a lambda expression.Examples of lambda expressions are performed in the following two common delegates.1.4 Action delegate and Func delegateWhen we consulted the MSDN Help documentation, we were able to see a lot of action<t> and func<t> in the form of code that had never been seen before. What the hell are they?Do not panic, they are entrusted, but they are more simple and more common.1.4.1 Action DelegateThe action delegate represents a method that references a void return type, and the parameters can be customized.If:action<int,string,bool> represents a void delegate with three parameters, we can use the definition and invocation of a lambda expression shorthand delegate:Action<int, string> action = (A, b) = = { Console.WriteLine (A.gettype (). ToString ()); Console.WriteLine (B.gettype (). ToString ()); }; Definition of Action Delegate Action (1, "1"); Invoking a Delegate objectProgram output:System.Int32System.String1.4.2 Func DelegateThe Func<t1,t2,t3> delegate represents a delegate with 2 parameters that returns the T3 type, that is, the last parameter is the return type, and all preceding parameters are parameter tables, see the following code.Func<int,string,bool> function= (A, b) = { Console.WriteLine (A.gettype (). ToString ()); Console.WriteLine (B.gettype (). ToString ()); return false; }; function (1, "1");Program output:System.Int32System.StringEntrust this knowledge point to belong. NET advanced technology, so for beginners to understand and apply it is a bit difficult, the more unfamiliar, the more retreat practice, because this is the only way to repair the immortal Buddha.Finally, we recommend a book that I think can be recommended 1000 times 10,000 times, this book is called C # Advanced programming, book author is the United States Christian Nagel,bill Evjen and Jay Glynn, Chinese version by Li Ming Translation, why recommend this book? Because this book contains almost everything. NET knowledge points, the explanation is also very in place, now published to the eighth edition, the web has the seventh version of the Chinese version and the eighth version of the English version, to see which version of your preferences.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.