The anonymous method in C,

Source: Internet
Author: User

The anonymous method in C,

The anonymous method in C # was introduced in C #2.0. It ended the era when C #2.0 and earlier versions declared that the only way to delegate was to use the naming method. Although in C #3.0 and later versions, Lambda expressions replace anonymous methods and are the preferred method for writing Inline code. However, the information about anonymous methods also applies to Lambda expressions. Lambda expressions are evolved from anonymous methods. This will be explained in the subsequent blog. You can use the anonymous method to ignore the parameter list. This means that the anonymous method can be converted to a delegate with various signatures, which is impossible for Lambda expressions. Learn the anonymous method to better understand Lambda expressions.

Before using the anonymous method, let's talk about other names of the anonymous method. Anonymous methods are also called anonymous delegation and anonymous functions. Although they are widely used, they are quite different. The msdn official website said: to pass a code block as a delegate parameter, creating an anonymous method is the only method. Here, the anonymous method is the official statement, and because the anonymous method is used to pass the code block as the delegate parameter, some people also call anonymous delegation, including the name I like, I think this is easy to understand. As for anonymous functions, the concept of "Method" in C # is called a "Function" in some languages. Therefore, an anonymous Method is also called an anonymous Function. However, in the msdn document, anonymous functions include Lambda expressions and anonymous methods. It can be said that anonymous functions are at a higher level, so the most official method is anonymous, of course, the other naming conventions are also widely spread, so you can understand them. There is no need to tangle.

Next, let's talk about the writing rules of anonymous methods. First, let's take an example:

delegate(int i) { return i > 0; }

It follows this format: delegate (form parameter table) {method body code}, which can be remembered by analogy with the writing of anonymous functions in js.
So where is the anonymous method used? How to use it? When you need a temporary method, the method is rarely used or the code of the method you need is very short, you can use the anonymous method. For a simple example, if you need to filter out a new set that meets the conditions in an integer set

List <int> list = new List <int> () {1, 2, 3, 4, 5, 6 }; // assume that all elements greater than 3 need to be obtained from the list set and var newlist = newlist is returned from the new set. findAll (GetNewList );

GetNewList () is a separately defined and delegated Predicate <T> method with the same signature (Predicate <T> is a built-in Delegate of the system, which will be discussed later in the blog)
GetNewList () is defined as follows:

bool GetNewList(int i)    {         return i > 3;    }

The above is not written when the anonymous method is used. If the anonymous method is used, you will find everything becomes so simple,

List<int> list = new List<int>() { 1, 2, 3, 4, 5, 6 };var newlist = list.FindAll(delegate(int i) { return i > 3; });

By comparison, we can find that the anonymous method can provide the same functions as the previous naming method, but it no longer requires a method that is explicitly created before it is associated with the delegate, therefore, the coding system overhead required to instantiate the delegate is also reduced, which is the biggest benefit.

 


In C language-> what?

-> Is a whole. It is used to point to a struct, class in C ++, and other pointers containing sub-data to obtain sub-data. In other words, if we define a struct in C and declare a pointer pointing to this struct, we need to use "->" to retrieve the data in the struct using the pointer ".
For example:
Struct Data
{
Int a, B, c;
};/* Define struct */
Struct Data * p;/* define struct pointer */
Struct Data A = {1, 2, 3};/* declare variable */
Int x;/* declare a variable x */
P = & A;/* point p to */
X = p-> a;/* indicates that the data item a in the struct pointed to by p is assigned to x */
/* Because p points to A, p-> a = A. a, that is, 1 */

For the first problem, p = p-> next; this should appear in the linked list of C language. next here should be a struct pointer of the same type as p, and its definition format should be:
Struct Data
{
Int;
Struct Data * next;
};/* Define struct */
............
Main ()
{
Struct Data * p;/* declare the pointer Variable p */
......
P = p-> next;/* assign the value in next to p */
}
The linked list pointer is a difficulty in C language, but it is also the key. It is very useful to learn it. To be careful, you must first talk about variables and pointers.
What is a variable? The so-called variables should not be simply thought that the amount will become a variable. Let's use the question of our Dean: "Is the classroom changing ?" Change, because there are different people in the classroom every day, but they do not change, because the classroom is always there, and it does not become larger or smaller. This is the variable: There is a constant address and a variable storage space. Under normal circumstances, we only see the variable in the room, that is, its content, but do not pay attention to the variable address, but the C language pointer is the address of the room. We declare that variables are equivalent to building a house to store things. We can directly watch things in the house, while declaring pointers is equivalent to getting a positioner. When a pointer points to a variable, it is to use the pointer to locate the variable. Then we can use the pointer to find the variable "tracked" and get the content in it.
What about struct? The structure is equivalent to a villa composed of several houses, and several houses are bound for use together. Suppose there are many such villas distributed in a big maze, and each villa has a house. The location information of another villa is put in it. Now you have found the first villa with the positioner and obtained what you want from it (the data part of the linked list ), then, calculate the location of the next villa into your positioner (p = p-> next), and go down to the next villa ...... If you go on like this, you will know that the information of a villa on the ground is gone (p-> next = NULL), and your trip is over. This is the process of traversing a linked list. Now you can understand the meaning of p = p-> next!
Write so much. I hope you can understand.
If you want to learn c and C ++ well, you must be familiar with linked lists and pointers!

In C language-> what?

-> Is a whole. It is used to point to a struct, class in C ++, and other pointers containing sub-data to obtain sub-data. In other words, if we define a struct in C and declare a pointer pointing to this struct, we need to use "->" to retrieve the data in the struct using the pointer ".
For example:
Struct Data
{
Int a, B, c;
};/* Define struct */
Struct Data * p;/* define struct pointer */
Struct Data A = {1, 2, 3};/* declare variable */
Int x;/* declare a variable x */
P = & A;/* point p to */
X = p-> a;/* indicates that the data item a in the struct pointed to by p is assigned to x */
/* Because p points to A, p-> a = A. a, that is, 1 */

For the first problem, p = p-> next; this should appear in the linked list of C language. next here should be a struct pointer of the same type as p, and its definition format should be:
Struct Data
{
Int;
Struct Data * next;
};/* Define struct */
............
Main ()
{
Struct Data * p;/* declare the pointer Variable p */
......
P = p-> next;/* assign the value in next to p */
}
The linked list pointer is a difficulty in C language, but it is also the key. It is very useful to learn it. To be careful, you must first talk about variables and pointers.
What is a variable? The so-called variables should not be simply thought that the amount will become a variable. Let's use the question of our Dean: "Is the classroom changing ?" Change, because there are different people in the classroom every day, but they do not change, because the classroom is always there, and it does not become larger or smaller. This is the variable: There is a constant address and a variable storage space. Under normal circumstances, we only see the variable in the room, that is, its content, but do not pay attention to the variable address, but the C language pointer is the address of the room. We declare that variables are equivalent to building a house to store things. We can directly watch things in the house, while declaring pointers is equivalent to getting a positioner. When a pointer points to a variable, it is to use the pointer to locate the variable. Then we can use the pointer to find the variable "tracked" and get the content in it.
What about struct? The structure is equivalent to a villa composed of several houses, and several houses are bound for use together. Suppose there are many such villas distributed in a big maze, and each villa has a house. The location information of another villa is put in it. Now you have found the first villa with the positioner and obtained what you want from it (the data part of the linked list ), then, calculate the location of the next villa into your positioner (p = p-> next), and go down to the next villa ...... If you go on like this, you will know that the information of a villa on the ground is gone (p-> next = NULL), and your trip is over. This is the process of traversing a linked list. Now you can understand the meaning of p = p-> next!
Write so much. I hope you can understand.
If you want to learn c and C ++ well, you must be familiar with linked lists and pointers!

Related Article

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.