我对字符串有一个疑问。 我们如何获取字符串的记忆?字符串修改值
是字符串是引用类型还是值类型?
1)如果它是参考类型然后执行下列操作的代码
List<String> strLst = new List<string>() { "abc","def","12"};
strLst.ForEach(delegate(string s)
{
if (s == "12")
{
s = "wser";
// I doubted whether = operator is overloaded in string class
//StringBuilder sb = new StringBuilder(s);
//s = sb.Append("eiru").ToString();
s = String.Concat(s, "sdf");
}
});
参见串的该值不被改变。我的问题是为什么字符串值没有改变? 如果它是引用类型,那么应该更改字符串值。
class emp
{
public string id;
}
List<emp> e = new List<emp>() { new emp() { id = "sdf" }, new emp() { id = "1" }, new emp() { id = "2" } };
e.ForEach(delegate(emp em)
{
if (em.id == "1")
em.id = "fghe";
});
这里值被改变,因为emp
是引用类型
2)如果string
是值类型
public sealed class String : IComparable, ICloneable, IConvertible, IComparable<string>, IEnumerable<char>, IEnumerable, IEquatable<string>
那么为什么他们mentione该字符串是一类?
请注意这是C#,而不是Java ... :) – Noldorin 2009-09-07 09:30:41
我的不好:)但我留下了答案,因为这个概念适用。 – 2009-09-07 09:32:20