struct string 和引用本身在传递的时候都会使用它的副本,要是闲的蛋疼,还可以这样用var s = new StringWrapper('123'); ArrayList test = new ArrayList(); ArrayList.Add(s); s.Value = "234"; Console.Write(test[0]);
我这样用的原因是因为,在form或者web编程中有时候需要批量赋值,有时候要单个赋值,把它添加到list在在批量赋值的时候很方便,但是就是会出现这种不能“同步修改”的问题 反正自己写了个类来实现了。 public class MyString { private string inner = ""; public string Value { get { return inner; } set { inner = value; } } public MyString(string _s) { inner = _s; }
ArrayList test = new ArrayList();
ArrayList.Add(s);
s.Value = "234";
Console.Write(test[0]);
反正自己写了个类来实现了。
public class MyString
{
private string inner = "";
public string Value
{
get
{
return inner;
} set
{
inner = value;
}
} public MyString(string _s)
{
inner = _s;
}
public static string operator +(MyString _ms, string _s )
{
return _ms.Value + _s;
} public static string operator +(string _s, MyString _ms )
{
return _s + _ms.Value;
}
}
按照你的说法,所有的值类型(例如int)是不是也都得弄这一套?
fixed (char* ss = s, ss1 = s1)
{
for (char* write = ss, read = ss1, end = ss + s.Length; write != end; *write++ = *read++) ;
}
Console.Write(s);
ref与out如果被内联优化不分配内存,否则传递对象的地址(就是一个指针)。但是是否内联是不可控的。
fixed (char* ss = s, ss1 = s1)
{
for (char* write = ss, read = ss1, end = ss + s.Length; write != end; *write++ = *read++) ;
}
Console.Write(s);对于静态常量做指针修改一定要知道,由于.net字符串池的存在,这个修改将对所有相同值的静态常量起作用,而且还会造成这个静态常量在字符串池中的hash错乱。所以,不要轻易对静态常量字符串做指针修改操作。
unsafe {
// 这里可以用指针:
int * p = xxx;
}
.NET字符串常量保存在一个字符串池中,作用类似于常量区。但是不仅仅是常量,你可以手动将非常量字符串添加到这个池中以共享内存。