string a="a";
string b="b";swap(a,b);  //类似的实现都结果 a="b";b="a";

解决方案 »

  1.   

    public void swap(ref string a, ref string b)
    {
       string c;
       c = b;
       b = a;
       a = c;
    }
      

  2.   

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

  3.   

    楼主想干什么?简单的交换没什么意思吧,记得很久以前.NET版有过这么一个帖子,好像说是不让用中间变量,看谁的最短之类的,还讨论了好几百楼...
      

  4.   

     void swap(ref string a, ref string b)
            {
                string temp;
                temp = a;
                a = b;
                b = temp;
            }
      

  5.   

    http://topic.csdn.net/u/20070314/18/6bc8e8ce-ca11-4781-a100-237bceeb9311.html
      

  6.   

    不知意图何在我承认是冲分来的。呵呵
     public void swap(ref string a, ref string b)
     {
         a = a + b;
         b = a.Substring(0, a.Length - b.Length);
         a = a.Substring(a.Length - 1);
     }
      

  7.   

    我就操了 今天面试就压根没想起ref 
      

  8.   

    改下
    public void swap(ref string a, ref string b)
    {
         a = a + b;
         b = a.Substring(0, a.Length - b.Length);
         a = a.Substring(a.Length - b.Length);
    }