tags:

views:

265

answers:

4

Is there CIL instruction to exchange the first two elements in the stack?

+4  A: 

There is no single instruction exchange. However, using stloc, pop, and ldloc, you should be able to accomplish your exchange.

sixlettervariables
+1  A: 

Looking at a list of CIL instructions there doesn't appear to be a single instruction that exchanges the two elements at the top of the stack. You'll have to do it the old pop/push way.

Chris Charabaruk
A: 

For future reference, you can create an assembly that does what you want to learn the IL for, then view the assembly in Reflector. You can select the language you wish the code to be in, and IL is one of the options. I did this when trying to figure out how to code a dynamic method...

Will
A: 

No. The only way to swap elements is to pop the top two elements to locals, then push them in reverse order.

Nick Johnson