string是引用类型,为什么输入结果是hello 而不是helloworld呢?using System;
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "hello";
            string ss = str;
            str += "world";
            System.Console.WriteLine(ss);
        }
    }
}

解决方案 »

  1.   

    哥哥,你输出的是ss 啊,好好看看代码
    其实str 变成了helloworld
    而ss 还是刚开始被赋给的hello 啊 
      

  2.   

    输入结果?输出结果吧!str+="world";这一句执行之后生成了一个新的字符串,str现在指向这个新的字符串,而ss指向原来的字符串。
    要注意,在.Net中,字符串是不变量,象这种类型的连接操作,每一次都会生成一个新的字符串,所以,在需要大量这种操作的时候,一般是使用StringBuilder类。
      

  3.   

    恩,我知道。string ss=str;这句 不就是说 ss和str一样了吗?这样,str变化,ss也该跟着变化啊?
      

  4.   


    namespace ConsoleApplication1 

        class Program 
        { 
            static void Main(string[] args) 
            { 
                string str = "hello"; 
                string ss = str; 
                str += "world"; 
                System.Console.WriteLine(str); 
            } 
        } 
    }
      

  5.   

    namespace ConsoleApplication1
    {
        class test {        int a = 0;
            public test(int a) {            this.a = a;
            }
            public  void getdata() {            Console.WriteLine(a);
            }
            public void setdata(int b){
                a+=b;
            }
            
      }
        
        class Program
        {
            static void Main(string[] args)
            {
                test t = new test(5);
                test tt = t;
                t.setdata (1);
                tt.getdata();        }
        }
    }这个和string的有些类似
      

  6.   

    不能看仔细一点儿吗?str+="world";这一句执行之后生成了一个新的字符串,str现在指向这个新的字符串,而ss指向原来的字符串。