Learn C # You should be familiar with using ILDasm and Reflector [with video tutorial ],

Source: Internet
Author: User
Tags cursor library reflector

Learn C # You should be familiar with using ILDasm and Reflector [with video tutorial ],

  

We usually have more or less contact with ILDasm and Reflector when learning C #. These two tools make us understand C # not just at the compiler level.

But let us penetrate the compiler more deeply. This article is also worth the attention of students who are not very familiar with IL and Reflector or are lazy.

Inform the two tools of their strength and necessity in the form of QA.

 

Q: Can an interface only contain methods?

 

A: If you are not familiar with IL, you may think it is wrong. You may naively think that, if you remember well, attributes can also be defined in interfaces.

It seems that the field can also be defined in the interface, but I don't know if I can. Well, let's take a look at what IL is like.

1     public interface IFly2     {3         bool CanFly { get; set; }4 5         event Action ActionFly;6     }

Then let's look at IL and we will find that the "attribute" is actually the GetXXX/SetXXX method. The "Event" is actually the Add_XXX/Remove_XXX method,

Of course, there is also an index, which is essentially the pairing method get_Item/set_Item. After reading these three attributes, some people may come up with another

"Constructor" is essentially a method, but it cannot be defined in an interface. Of course, this is not an IL thing, but an issue of OO definition, because the interface defines

The purpose is to standardize, while the ctor class is an initialization class, which is a kind of implementation of the class. The interface specification is to provide only the definition of the method, but not the implementation of the method. Now let's look at it again.

Can a field be defined in an interface?

We can clearly see that the error message "the interface cannot contain fields" is okay. Now we can sum up the following sentence: the interface can only define the except Constructor

This example shows the importance of IL to our problem analysis.

 

Q: Why does a New entry repeat appear multiple times after Random. Next?

        static void Main(string[] args)        {            for (int i = 0; i < 1000; i++)            {                var rand = new Random();                Console.WriteLine(rand.Next());            }            Console.Read();        }

 

A: This is A good question. If you want to know why, you must use Reflector to decompile the Random class code. How can this problem be implemented?

However, let's take a look at the decompiled code.

From the definition of the next function, we can see that the num value of return depends on seedArray, so let's take a look at how seedArray is played.

Through the code, we can find that it is actually done in the ctor.

We can see this for loop. Let's take a look at the code. We can see that the first for is to add a value to the seedArray, the second for is to take the subscript for the remainder, and then-= the operation.

To make the values in the SeedArray more chaotic. The general meaning is that the values in the SeedArray are chaotic and random, and then our next is through inext and inextp

Taking the SeedArray value, resulting in a random number, it is not difficult to see that the random source is the value we gave to the previous Seed, and then we can see that inext and inextp are both ++ operations,

Therefore, after multiple next operations, this ensures that the number is always random. If the source Seed is the same, the return value will always be repeated.

 

This time we put the Random Out Of The for loop and look at it again, because inext and inextp get the SeedArray value through the ++ operation to avoid repetition as much as possible.

 1         static void Main(string[] args) 2         { 3             var rand = new Random(); 4  5             for (int i = 0; i < 1000; i++) 6             { 7                 Console.WriteLine(rand.Next()); 8             } 9             Console.Read();10         }

 

 

Please slam the video tutorial: http://t.cn/RPtyOFp

 


How to Learn C language?

I have answered this question in the morning:
1: Don't worry, be patient, and think about what kind of system can be written.
2: some reference books are necessary, so you don't have to talk about them. For example, Tan haoqiang's C. You can use Baidu's related materials.
3: Do not worry about the case code in the book (instead of copying or sticking to the post, you can knock it out with one letter or one letter ).
4: run the code to make the correct results come out.
5: modify some code and try to run it. Finally, the modified Code can run correctly.
6: write your own code.
7: The above six steps are based on your understanding of some syntax and structure. If you have a base of 0, let's take a look.
8: In the order of books, first learn the syntax, structure, variables, etc., in learning arrays, pointers, linked lists and other knowledge.
9: Don't worry. Step 1 is an important foundation. It must not be sloppy.
10: Perform Step 3-6 exercises. Continue learning.
11: You can continue learning in step 1.
12: You can learn other books, such as data structures and algorithms.
13: Write and debug the code. Run, modify, complete, debug, run, Expand functions, debug, run .....
14: Write and debug code. Run .....
Programming requires a lot of code exercises, and it is useless to talk about it on paper. Be sure to do it, debug it, and run it.
The above knowledge is only the basic part of C. You can continue to learn C ++ and Java in the future. Of course, you can also continue to learn the advanced part of C, such as the network and cursor library ).
Taking the path of programming, light learning language is not enough, such as algorithms, compilation principles, software engineering, computer systems, high numbers, physics, and so on.
Hope to help you

What is the basis for learning C language?

A certain degree of English is necessary.

When reading a book, you must do more exercises. You can understand one question or one question.

From the very beginning, I got into good programming habits. How can I write the code on your books?

With regard to mathematics, the level of mathematics in junior high school can be programmed, and there is nothing wrong with elementary school or even preschool. On the other hand, when programming with a doctor in mathematics, he will not think that he has learned too much in mathematics. The amount of mathematics required is closely related to what you need to do. It also affects the depth of your learning and affects the quality of your code at work. In general, the higher the level of mathematics, the better, but there is no threshold for learning any level, that is, the quality is poor.

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.