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 only to contact with the people, and the customs also have a lot of guards. Also, whether they are basketball or football players, they have to do a very long period of physical exercise, such as running, before they really touch the ball.
Suppose we want to become the martial arts of the procedural world, then we must also retreat to practice, meticulous cultivation of basic skills.
So what do we do with Windows Mobile development? What are the basic skills of apes? In fact, there are a lot of basic skills, the most important is the ability to think logically and solve this problem, then again, logical thinking and solve the problem of the ability to come from? 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 cultivate entrusted, inheritance, polymorphism, garbage collection and generics, this time, I mainly practice in C # entrusted.
1.1 What is entrustedPeople who have studied C and C + + know pointers, and when we want to pass the method as a reference, we are able to pass a pointer to the function, that is, the memory address of the function, which causes the type to be unsafe and very difficult to control, in C #, in order to avoid this problem, the surrender is a special type of object, It and the class should be level, that is, the trust can be declared within the namespace, can also be declared within the class, the trust is a memory address, but it is type-safe.Statement of 1.1.1 EntrustedIn the same way as the class, it is necessary to declare in advance the format of the declaration:delegate int Dgetresult (int a,int b);Statement of 1.1.2 Entrusted instanceDeclaration of good one entrusted, we need to give him a method instance, the method instance must be signed with the same as the trust, that is, the method return value type and the trust agreement, the type, number, order and trust of the method parameters. An example method that is entrusted above:private int Getaddresult (int a,int b){return a+b;}Below, we are able to invoke this trust in the main function, call the Commit and class, also must apply for a entrusted object, through the object invocation, such as the following:Dgetresult getresult = Getaddresult; There is no need to add a number and parentheses, the address of the entrusted object to point to the methodConsole.WriteLine (GetResult (2,4).   ToString ()); Run through entrusted objectsConsole.readkey ();Assignment of 1.1.3 entrusted object, addition and subtraction of methodA entrusted object can call multiple methods at the same time, we can add multiple instance methods to a entrusted object, such as the following: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, assuming we run the breakpoint inside the method Getaddresult, we will find that the method Getaddresult is also running.Since the entrusted object can join the method, the entrusted object can also delete the method.Gerresult-=getminusresult;Console.WriteLine (GetResult (2,4). ToString ());The result of this procedure is 6.1.1.4 Multicast Trust method blocked problemIn fact, the trust we used above is called Multicast trust, multicast Trust has a problem, when the Multicast Trust method group in the previous method of an exception, then the method of the Trust object will stop the iteration, the subsequent method will not be run, 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 entrusted object returns an array of delegate objects through the Getinvocationlist () method, and then iterates through the delegate array, making its own definition, so that the previous method is abnormal, and the entrusted object will iterate the method. The latter method will not be blocked by the previous exception.1.2 Why do we have to entrustFirst, the trust is type-safe, trust easy control, high efficiency; In addition, the use of trust in large applications reduces 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, such a syntax can only use super cool two words to get used to, this is the C # to his program ape to open the Comparisonof, C # program apes must be happy for it.Lambda expressions consist of: (1, Parameter 2 ...) =>{detailed implementation};All of the types of commit parameters are able to use lambda expressions, and the anonymous method assumes that it should be a good thing to write with a lambda expression.Examples of lambda expressions are made in the following two frequently used trusts.1.4 Action entrusted and Func entrustedWhen we look at the MSDN Help documentation, we can see a lot of action<t> and func<t> in the form of code that has never seen such a structure before. What exactly are they?Do not panic, they are entrusted, just they are more simple and more often used.1.4.1 Action entrustedAn action delegate represents a method that references a void return type, and the parameters can be defined by themselves.If the:action<int,string,bool> represents a void commit with three parameters, we can use the definition and invocation of the lambda expression shorthand:Action<int, string> action = (A, b) = = { Console.WriteLine (A.gettype (). ToString ()); Console.WriteLine (B.gettype (). ToString ()); }; Definition of Action Commit Action (1, "1"); Call the Entrusted objectProgram output:System.Int32System.String1.4.2 Func entrustedFunc<t1,t2,t3> entrust represents a delegate with 2 parameters that returns the T3 type, that is, the last parameter is the return type, and all the previous references are the 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 people who just started learning 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 fairy into the Buddha.Entrust Summary:
Finally, to recommend a book that you think can recommend 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? As the book almost contains all. 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 number to see 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.