Learn New --- re-read C # InDepth (2 ),

Source: Internet
Author: User

Learn New --- re-read C # InDepth (2 ),

A good book, or a deep book, is a new discovery every time you study it.

Okay, I admit that every time I read it, there is a general suspicion that it is always possible ~~

Over the past few years, I have been focusing on the development of the C # client. Gradually, I am confused, confused, and self-righteous. Finally, I am down to review it. Maybe this is also a step-by-step process of self-learning.

I have mentioned so many things before, and I hope that you will not be so impetuous and "deeply understand" C # Every knowledge point of this language. This article summarizes the knowledge in books and provides an overview based on actual application scenarios. If there are any errors, please kindly advise.

The content in this article is relatively simple. Please skip this article.

 

4. Program Closure

The problem with program closure is that the program prejudges and processes certain variables (for personal understanding, please correct me if there are errors or deficiencies) so that some variables are supposed to be used as the value type but changed to the reference type, resulting in data exceptions.

Of course, in most cases, we will not encounter such a problem, but in some cases, we have to pay attention to and analyze the root cause of the problem, and the BUG will never be random.

The following describes the concept of closure through several examples:

Example 4.1

Private void button#click (object sender, RoutedEventArgs e) {int outerVariableCaptured = 5; // external variable (captured) int outerVariableUnCaptured = 50; // external variable (not captured) if (DateTime. now. hour <= 24) {int normalLocalVariable = 1; // local variable of the common method, not an external variable, because there is no anonymous method in its scope. This. txb_Msg.Text + = string. format ("local variable of normal method = {0}", normalLocalVariable) + System. environment. newLine;} Action x = new Action () => {int anonLocal = 2; // The local variable of the anonymous method this. txb_Msg.Text + = string. format ("anonymous method local variable = {0}", anonLocal) + System. environment. newLine; this. txb_Msg.Text + = string. format ("external variables captured in the anonymous method = {0}", outerVariableCaptured) + System. environment. newLine; // The variable outside the scope is called in the anonymous method, so the variable becomes the captured external variable}); this. txb_Msg.Text + = string. format ("uncaptured external variables of common methods = {0}", outerVariableUnCaptured) + System. environment. newLine; x ();}
Output result: local variables of common methods = 1 uncaptured external variables of common methods = 50 local variables of anonymous methods = 2 external variables captured in anonymous methods = 5

In section 4.1, we will help you understand external variables, local variables, and capture concepts. The most important is the captured external variable.

Example 4.2

Private void Button3_Click (object sender, RoutedEventArgs e) {// proof that the declared cycle of the captured local variable is extended. OnCreateDelegate + = MainWindow_OnCreateDelegate; this. Dispatcher. Invoke (OnCreateDelegate (this); // Invoke may be ambiguous here: it is because an event is returned after the Invoke event. // Counter is a value type. Data on the stack will be recycled when it escapes its scope. Is this true? // It is also illustrated from the other side that whether the value type is on the stack or on the stack depends on the type of the object to be created. }
Private Delegate MainWindow_OnCreateDelegate (object sender) {var frm = sender as MainWindow; int Counter = 1; var a = new Action () => {frm. txb_Msg.Text + = string. format ("delegate internal variable value = {0}", Counter) + System. environment. newLine; Counter ++;}); a (); return ;}
Output: delegate internal variable value = 1 delegate internal variable value = 2

In this example, the original life cycle of Counter's value type should be in the MainWindow_OnCreateDelegate (object sender) method, but it has escaped the scope of the method, this is what we call the value type on the stack maritime stack.
It depends on whether the initialization is on the stack or on the stack.

 

Example 4.3

Private void Button4_Click (object sender, RoutedEventArgs e) {// more complex situations var methods = new Action [2]; int outside = 10; // instantiate the variable once for (int I = 0; I <2; I ++) {int inside = 100; // instantiate the variable multiple times methods [I] = new Action () => {this. txb_Msg.Text + = string. format ("Inside Value = {0}; Outside Value = {1}", inside, outside) + System. environment. newLine; inside ++; outside ++; // variables captured by the anonymous method});} methods [0]. invoke (); methods [0]. invoke (); methods [0]. invoke (); methods [1]. invoke ();}
Output result: /*************** Outside variable Memory Sharing *************** Inside Value = 100; outside Value = 10 * Inside Value = 101; Outside Value = 11 * Inside Value = 102; Outside Value = 12 * Inside Value = 100; outside Value = 13 ************************************ ********/

In this example, you have to think about what the values of outside and inside are? Why?
The reason is that inside is initialized multiple times in the For loop. That is to say, For a few times, there are several independent inside objects, although it is a value type.

 

Example 4.4

This example is excerpted from writing high-quality code: 157 suggestions for improving C # programs

Private void Button5_Click (object sender, RoutedEventArgs e) {// closure trap var methods = new Action [2]; for (int I = 0; I <2; I ++) {int inside = I; // instantiate the variable multiple times methods [I] = new Action () => {this. txb_Msg.Text + = string. format ("Inside Value = {0}; Index Value = {1}", inside, I) + System. environment. newLine;});} methods [0]. invoke (); methods [1]. invoke (); /***************** closure trap ******************** ** When I is used, I is the shared variable (captured external variable), so the maximum value of I is always output. * When the Inside value is used, the Inside variable is an internal variable. Every time an object is created, the inside value is generated again. Therefore, the Inside value increases progressively, that is, the value of I in the cache. * For IL, it creates Tempclass. I to replace I, resulting in I value sharing. **************************************** ****/}
Output result: Inside Value = 0; Index Value = 2 Inside Value = 1; Index Value = 2

In fact, if we use ILDasm, for the I object, IL generates a DisplayClass (that is, a name) class, which is the most common cause: I changes to the reference type, and the data is abnormal.

 

 

Continuous update: Sample Code download


Help me, ask what is re-reading and non-re-reading in English? The better.

The pronunciation rules of English words re-reading rules are mainly for re-reading syllables. Therefore, how to determine the re-reading syllables of two-syllable words and multi-syllable words, it becomes the key to implementing the word "seeing is capable of reading.

Which of the two-syllable and multi-syllable words re-reading often involves the structure of words, so it is necessary to first introduce the main word building methods.

There are many ways to form English words, but there are three main methods: (1) the fix is legal: the prefix or suffix is used to form another word. For example: luck (luck) → lucky (lucky), unluky (unfortunately ). (2) Conversion Method: converts one word class to another. For example, water (n. water) → water (v. Watering ). (3) Synthesis: two or more words are combined into one word. For example: book + bag → bookbag (bag); mother + in + law → mother-in-law (mother-in-law ).

Ii. Rereading of two-syllable words

(1) general tendency of two-syllable word re-reading

1. Two-syllable verbs tend to repeat the second syllable. For example, absorb, behave, forget, and receive ).

2. disyllabic words other than verbs tend to repeat the first syllable. Example: custom (n. habits), distant (adj. distant), second (num. (2), terminate (pron. many), often (adv. frequently), into (prep. enter), after (conj. ...... Later ).

Suffixes of nouns, adjectives, and adverbs are mostly unrereded, such as-er,-or,-ess,-ism? -Tion? -Sion,-ssion? -Ment? -Ship? -Hood? -Age? -Ure? -Dom? -Ey? -Ance? -Our? -Ent? -Ace? -Ow? -Noun suffixes such as ic (s);-less? -Ish? -Ive? -Ous? -Able? -Visible? -Ic? -Some? -Like? -En-ed? -Al? -Ant,-ful? -Ing? -Ty? -Suffixes of adjectives such as ly. -Ly? -Ward (s),-wise, and other adverbs suffix. This further increases the tendency of such words to repeat the first syllable. Just a few examples. Nouns: actor, nation (country), adjective: useful (useful), dirty (dirty), adverbs: really (true), and number words: twenty (20 ).

3. Some words can be both verb and noun or adjective. When a verb is used, the second syllable is reread. When a noun or adjective is used, the first syllable is reread. This shows a clear comparison between the two tendencies. For example, when-rekord is used as the verb "record", read [ri 'k? : D) read [-rek? D ?; Present reads [prizent] As the verb "present" and ['prez? Nt 〔? B 'sent, which is used as the adjective "absent 〔′? Bs? Nt 〕.

(2) Factors Affecting the word-building tendency of double-syllable words

The two-syllable verb tends to reread the second syllable, while other double-syllable words tend to reread the first syllable, but this does not mean that all double-syllable words are in this way. According to statistics, 90% of the two-syllable verbs conform to the tendency of re-reading the second syllable, and about 60% of the non-verb two-syllable words conform to the tendency of re-reading the first syllable.

The re-reading of two-syllable words is also affected by the word structure.

1. Influences of suffixes

① With-en? -Er-,-ish,-le,-y,-ow, and other non-re-reading suffixes. The second syllable is not re-stressed, and the first syllable is re-stressed. For example, happen, open, finish, struggle, carry, swallow, and borrow ).

② Adverbs with Prefix a-, be-, to-, ex-, and so on, adjectives, prepositions, hyphens, and nouns ending with-ee and-self ...... remaining full text>

New lyrics

Song name: Learn New
Artist: farmer
Album: fengshengshuiqi

Farmer-learn new
Composing: DJ Galaxy: C jun, 6 Yong
Farmer-learn new
ONE TWO THREE
456 luwing
This time does not matter
I can hear it clearly next time.
This time is not so important
You have to be nice.
Oh is called sunshine food
Lalalalalala lalala
Wohoho ~)
You love a plane, I love a cup.
You love me, Sissy, I love you, sister.
You should love Li Ying love LI yingzai
Shouldn't it be true
456 Edison Chen
Red or red doesn't matter
In my moments, it's like Zuo jieqing.
Love, love, and love are priceless.
Love me words true love without price
First, you must have a big figure.
What's more, nurse manager
It's better to have a driver in a car
Love me first
Love is a reality
You have no car, no floor, no dog, you need to drag my hand
Women do not have products, men do not have silver
So hundreds of interest cards are better than platinum accessories
Lin zixiang, you are yourself, er, yourself.
What about Edison Chen? d. Both female and female have errors.
But at home, my girlfriend is really a d.
Don't you need silver paper or Dior?
Net wants to take care of me
If there is an affair, you will not trust apple.
I am not angry because I have a lot of time.
Love is all
Seeing you again
Every time you ask, you are the fastest way out of the world.
Every time you give a gift, you never give up.
At the very end, I love you.
You need to know something.
I want to eat Sushi later. I want to go through the female toilet.
Seeing a lady living in a white skirt
When talking about me funny, I will be able to change the phone address instantly.
Please say goodbye.
I want to see it now
I am going to join you tonight
Later with him
STOP ......
Stride forward
Your Own Path
Well, it's a good place to walk with your shoes.
NO! I recommend that you
I am not tied to the rap Table Category
I use less than half of my d force
My net series is intended for you, ladies and gentlemen.
Thank you!
It's a song, giving me a glimpse of your music
ByeBye ~

Music.baidu.com/song/1180442

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.