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);
}
}
}
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);
}
}
}
其实str 变成了helloworld
而ss 还是刚开始被赋给的hello 啊
要注意,在.Net中,字符串是不变量,象这种类型的连接操作,每一次都会生成一个新的字符串,所以,在需要大量这种操作的时候,一般是使用StringBuilder类。
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string str = "hello";
string ss = str;
str += "world";
System.Console.WriteLine(str);
}
}
}
{
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的有些类似