locked
Swap method RRS feed

  • Question

  • Hi,

    Anyone knows if there is a generic Swap method in .NET? If not, then anyone knows why?

    I always use it and it bothers me to have it hanging around in every code I write. like an external thing.




    Thanks.
    Thursday, October 9, 2008 11:26 PM

Answers

  • There is probably not as much of a need for it as you have.  I have rarely had to use something like this in most of the things I've written.   Just out of curiosity, why do you need something like this?  I see it implemented only once as a protected property of TextBoxBase, and only for Int32 types.
    David Morton - http://blog.davemorton.net/
    • Marked as answer by Harry Zhu Monday, October 13, 2008 3:21 AM
    Friday, October 10, 2008 4:49 PM
  • Darren Oster and parinavv007 said:

    void Swap<T>(T a, T b)  
    {  
        T c = a;  
        a = b;  
        b = c;  



    This method will not work for value types at all. For it to work properly, the ref keyword will have to be used:

    public static void Swap<T>(ref T a, ref T b)  
    {  
        T c = a;  
        a = b;  
        b = a;  

    David Morton - http://blog.davemorton.net/
    • Marked as answer by Joan Venge Tuesday, October 14, 2008 4:06 PM
    Friday, October 10, 2008 5:21 PM
  • That's true.

    I wrote something like this:

        public static void Swap<T> ( ref T lhs, ref T rhs )
        {
            T temp = lhs;
            lhs = rhs;
            rhs = temp;
        }






    • Marked as answer by Joan Venge Tuesday, October 14, 2008 4:07 PM
    Friday, October 10, 2008 5:23 PM

All replies

  • What are you generically Swapping?  Do you mean something like the following?

    void Swap<T>(T a, T b)  
    {  
        T c = a;  
        a = b;  
        b = c;  
    • Proposed as answer by Tergiver Friday, October 10, 2008 4:56 PM
    • Marked as answer by Harry Zhu Monday, October 13, 2008 3:21 AM
    • Unmarked as answer by Joan Venge Tuesday, October 14, 2008 4:06 PM
    Friday, October 10, 2008 2:08 AM
  •  
    void Swap<T>(T a, T b)  
    {  
        T c = a;  
        a = b;  
        b = c;  
    do you want to like this
    Friday, October 10, 2008 3:04 AM
  • Thanks, yes I wrote the same one, but why something like this isn't included in the .NET framework?





    Thanks.
    Friday, October 10, 2008 4:36 PM
  • There is probably not as much of a need for it as you have.  I have rarely had to use something like this in most of the things I've written.   Just out of curiosity, why do you need something like this?  I see it implemented only once as a protected property of TextBoxBase, and only for Int32 types.
    David Morton - http://blog.davemorton.net/
    • Marked as answer by Harry Zhu Monday, October 13, 2008 3:21 AM
    Friday, October 10, 2008 4:49 PM
  • Simple :)

    In some graphics algorithms you have to swap things to make the code "simpler".

    Say for something like this.

    But I guess you are right, it doesn't cater to everyone using the .NET framework. Still would be nice I think.





    Thanks.
    Friday, October 10, 2008 5:13 PM
  • Darren Oster and parinavv007 said:

    void Swap<T>(T a, T b)  
    {  
        T c = a;  
        a = b;  
        b = c;  



    This method will not work for value types at all. For it to work properly, the ref keyword will have to be used:

    public static void Swap<T>(ref T a, ref T b)  
    {  
        T c = a;  
        a = b;  
        b = a;  

    David Morton - http://blog.davemorton.net/
    • Marked as answer by Joan Venge Tuesday, October 14, 2008 4:06 PM
    Friday, October 10, 2008 5:21 PM
  • That's true.

    I wrote something like this:

        public static void Swap<T> ( ref T lhs, ref T rhs )
        {
            T temp = lhs;
            lhs = rhs;
            rhs = temp;
        }






    • Marked as answer by Joan Venge Tuesday, October 14, 2008 4:07 PM
    Friday, October 10, 2008 5:23 PM
  • Fair call.  Back to the original question, I guess the issue is, where would you put something like this?  What namespace? Within which class?  For the sake of such a small amount of code, the process of putting it in the .NET framework would actually be fairly complicated.  You would almost need to introduce a "generic" or "common functions" Generic class, which would then need to be instantiated for each Type you wanted to use the Swap method for, before calling a static Swap method of that instance.

    Also, its usefullness is limited without a bunch of supporting code, for example if you Swapped two objects in an ObservableCollection (or more likely, two objects contained by two items in an ObservableCollection), the xxChanged events may not trigger.  So, to save MS the hassle of implementing what could be very difficult to implement 'generically', I expect they leave it up to the programmer to implement their own, and then it's on the programmer's head to get the plumbing right.
    • Proposed as answer by Darren Oster Sunday, October 19, 2008 11:53 PM
    Friday, October 10, 2008 7:58 PM
  • I see, that was what I was wondering.

    A method like this would have to be global but I know in .NET, it has to live in some class, namespace, etc.

    I think that's the reason they didn't put it in, but not because it's not useful.




    Thanks.
    Friday, October 10, 2008 8:36 PM
  • Hi,

    Try this swapping good feature in Visual C#.

    a ^= b;

    b ^= a;

    a ^= b;

    Regards,

    Selva Ganapathy K

     

    Wednesday, April 15, 2015 12:37 PM
  • You are replying to a question that was answered almost 7 years ago. Your suggestion doesn't really answer the question, which was about a generic method (see the other answers for details).
    Wednesday, April 15, 2015 4:58 PM