C #4.0 syntactic sugar Article 5: Anonymous class & anonymous method

Source: Internet
Author: User

It was a little early today, so I am writing an article. Continue with the previous article. Have we ever encountered such a situation during programming, the classes you define are only used to encapsulate related data, but do not need associated methods, events, and other custom functions. At the same time, this class is only used in the current application, and does not need to be reused between projects. All you need is a "temporary" type. Now let's look at the definition of this traditional class:

1 internal class oneclass
2
3 {
4
5 // define several private data members
6
7 // encapsulate each data member through attributes
8
9 //...
10
11} view code

From the code build above, although this class is not difficult to say, if this class has many data members, it will take quite some time. To solve this problem, Microsoft proposed a new concept called anonymous type.

Anonymous type:The anonymous type provides a convenient method to encapsulate a set of read-only attributes into a single object without explicitly defining a type. The type name is generated by the compiler and cannot be used at the source code level. The type of each attribute is inferred by the compiler.

The following example shows the anonymous type initialized with two attributes named amount and message.

1 var v = new {amount = 108, message = "hello "};
2
3 console. writeline (V. Amount + "" + v. Message );
4
5 console. Readline (); view code

From this example, we can see that the anonymous type looks simpler and clearer than the traditional type.

Output result:

 

 

 

 

The anonymous type can also be nested. The Code is as follows:

1 var aemployee = new
2
3 {
4
5 joindate = "2007-7-31 ",
6
7 apeople = new {sex = "male", name = "Linc", age = "26 "},
8
9 Title = "projmanager"
10
11}; view code

This class is a simple example here. This class is used in many lambda expressions and can be understood more thoroughly when sharing lambda expressions.

Restrictions on using it:

1: The anonymous type does not support events, custom methods, and custom rewriting.

2: The anonymous type is implicitly closed (sealed)

3: Create an anonymous instance using the default constructor only

4: The anonymous type does not provide a class name for control (defined using VAR)

Anonymous method:A common method is defined. Because a method is used to reuse a piece of code, a name is usually given to the method. The reference of this method can be called by "method name. However, some methods do not need to be reused. They only need to be used once. Therefore, method names are not required. This method is called anonymous method. The anonymous method must be used in combination with the delegate. (Potentially, although there is no method name, the method pointer is stored in a delegate object)

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 in this topic also applies to lambda expressions. In one case, the anonymous method provides functions not available in lambda expressions. 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. This is not possible for lambda expressions.

We can see that all these estimates are in the fog. Let's get a deeper understanding of the Code:

First, let's write a simple example of a traditional method:

1 public class nnff
2
3 {
4
5 // define the delegate Signature
6
7 public delegate void printer (string S );
8
9 public static void Ceshi ()
10
11 {
12
13 // traditional mode
14
15 printer = sayhello;
16
17 printer ("hello ");
18
19}
20
21 public static void sayhello (string S)
22
23 {
24
25 console. writeline ("I am a normal method:" + S );
26
27}
28
29} view code

For example, the following sayhello method is only called in the Ceshi method. It is a little tedious to write a Method Out Of The Ceshi method when it is no longer used elsewhere. Download and let's take a look at the writing method of the anonymous method:

1 public class nnff
2
3 {
4
5 // define the delegate Signature
6
7 public delegate void printer (string S );
8
9 public static void Ceshi ()
10
11 {
12
13 // anonymous method
14
15 printer = delegate (string S)
16
17 {
18
19 console. writeline ("I am an anonymous method:" + S );
20
21 };
22
23 printer ("hello ");
24}
25
26} view code

From the above we can see that the method of output content is written into method Ceshi, so that this method is protected and cannot be called.

In the next two lessons, I will give a more specific example of lambda expressions. In fact, lambda expressions are actually anonymous. Abbreviated anonymous method!

Note:

1. After compilation, a private static method will be created for each anonymous method, and the static method will be passed to the delegate object for use.

2. Anonymous Method: After compilation, a delegate object is generated, a method is generated, a method is loaded into the delegate object, and a value is assigned to the declared delegate variable.

3. parameters can be omitted for anonymous methods: during compilation, parameters are automatically added for this method according to the parameters of the delegate signature.

 

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.