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

Source: Internet
Author: User

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

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 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.

 


C language ^ how to use

A1 = 0x01; // 0000 0001
A2 = 0x00; // 0000 0000
A3 = 0x03; // 0000 0011
A4 = 0x02; // 0000 0010

B1 = a1 ^ a2; // 0000 0001
B2 = a1 ^ a3; // 0000 0010
B3 = a1 ^ a4; // 0000 0011

^ XOR operator. The bitwise value is 0 and the difference is 1. See the example above.

//
Examples of simple and practical problems:
====================================
======= A ======= B =========
There are two circuits on the top. The two switches are a and B respectively. The opening status is \ [1], and the closing status is/[0].
If both circuits are enabled or disabled.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, the circuit fails in the and B states simultaneously [0]. When a and B are different, the power is charged [1].

Symbol in C Language <Yes

Left shift operator (<)

Removes all the binary bits of an operation object from the left and adds 0 to the right ).

For example, a = a <2 shifts the binary bits of a two places to the left and complements 0 to the right,

Move 1 to the left and then a = a * 2;

If the left shift does not include 1 in the Discard high position, then shifts one bit left, which is equivalent to multiplying the number by 2.
Shift right operator (>)

Shifts all the binary bits of a number to several places to the right, and adds 0 to the left of the positive number, 1 to the left of the negative number, and discards the right of the negative number.

The operand shifts one digit to the right, which is equivalent to dividing the number by 2.

For example, a = a> 2 shifts the binary bit of a two places to the right,

0 or 1 to see whether the number is positive or negative.

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.