Eric Smith
Eric Smith

Reputation: 5392

CIL stack exchange instruction

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

Upvotes: 11

Views: 1489

Answers (4)

Nick Johnson
Nick Johnson

Reputation: 101149

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

Upvotes: 2

user1228
user1228

Reputation:

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

Upvotes: 0

Chris Charabaruk
Chris Charabaruk

Reputation: 4417

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.

Upvotes: 1

user7116
user7116

Reputation: 64068

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

Upvotes: 11

Related Questions