C#: Good/best implementation of Swap method

前端 未结 6 1480
醉话见心
醉话见心 2020-12-06 10:33

I read this post about card shuffling and in many shuffling and sorting algorithms you need to swap two items in a list or array. But what does a good and efficient Swap met

6条回答
  •  無奈伤痛
    2020-12-06 11:14

    A good swap is one where you don't swap the contents. In C/C++ this would be akin to swapping pointers instead of swapping the contents. This style of swapping is fast and comes with some exception guarantee. Unfortunately, my C# is too rusty to allow me to put it in code. For simple data types, this style doesn't give you much. But once you are used to, and have to deal with larger (and more complicated) objects, it can save your life.

提交回复
热议问题