When we learn C #, we usually have more or less contact with ildasm and reflector, both of which make our understanding of C # not just on the compiler level.
, but let's go deeper through the compiler. This is also hope to IL and reflector not very understanding or lazy understanding of the students can pay attention to, the same article I still
In the form of QA to tell the two tools powerful and necessary.
Q: Is there only a method in the interface?
A: This problem, if you are unfamiliar with IL, you may think that is wrong, you may be naïve to think, remember good words, attributes can also be defined in the interface, things
Pieces can also be defined in the interface, the field may not know, OK, we look at IL, exactly what is.
1 Public InterfaceIFly2 {3 BOOLCanfly {Get;Set; }4 5 EventAction actionfly;6}
Then we look at IL, we will find the "attribute" actually getxxx/setxxx method, "event" is actually add_xxx/remove_xxx method,
Of course, there is an "indexer", which is essentially the pairing method of Get_item/set_item, after reading these three properties, some people may want to come out and have a
"Constructor", in essence it is also a method, but it is not defined in the interface, of course, this is not the IL thing, but the OO definition of the problem, because the interface definition of the
The purpose is to standardize, and ctor is the initialization class, which belongs to the implementation of the class, and the specification of the interface is only to provide the definition of the method, can not provide the implementation of the method, now we look at
, can the field be defined in the interface?
It can be clearly seen that the "interface can not contain fields" error information, well, now we can conclude that the interface can only be defined in addition to the constructor
Outside of the method, this example shows the importance of IL for our analysis of the problem.
Q:random.next Why do you repeat after New times?
Static voidMain (string[] args) { for(inti =0; I < +; i++) { varRand =NewRandom (); Console.WriteLine (Rand. Next ()); } console.read (); }
A: This question is good, want to know why, we must use reflector to decompile the code of the Random class, in the end is how to implement, to know
And then, to know why, let's look at the post-compilation code.
From the definition in the next function, we can see that the NUM value of return depends on the Seedarray, so we'll see how Seedarray actually plays.
Through the code, we can find what is actually done inside the ctor.
We can see this for loop, probably look at the code meaning, you can see that the first for is to give Seedarray value, the second for is to remove the mark is the remainder, and then-= operation
To let Seedarray in the value of more confusion, anyway, the general meaning is seedarray in the value of the chaotic, relatively random, and then our next is through Inext and INEXTP
Take the Seedarray value, resulting in random numbers, it is not difficult to see that the source of the random is the seed value we gave to the past, and then we see that Inext and INEXTP are + + operations,
So after we have done this several times, this guarantees that our numbers will always be random, and if the seed of the source is the same, it will cause the return value to repeat.
This time we take the random out of the for loop and look at it, because Inext and INEXTP are using the + + operation to get the value of Seedarray to avoid duplication as much as possible.
1 Static voidMain (string[] args)2 {3 varRand =NewRandom ();4 5 for(inti =0; I < +; i++)6 {7 Console.WriteLine (Rand. Next ());8 }9 Console.read ();Ten}
Copyright NOTICE: This article for Bo Master http://www.zuiniusn.com original article, without Bo Master permission not reproduced.
Learn C # You should be proficient in using ILDASM and reflector