如何通过引用将字符串传递到集合中?

发布于 2025-01-07 00:33:47 字数 577 浏览 0 评论 0原文

似乎字符串是引用,但按值复制

List<string> data = new List<string>();
string s = "a";
data.Add(s); // copy by value??
s = "b";
Console.WriteLine(data[0]); // "a"

它也似乎类是按引用复制的引用

class boxstring { public string val; }
List<boxstring> data = new List<boxstring>();
boxstring s = new boxstring { val = "a" };
data.Add(s); // copy by reference, ok
s.val = "b";
Console.WriteLine(data[0].val); // "b"

是否有一种更简单的方法可以通过引用将字符串传递到集合中而不将其包装到类中? 我想将对象成员传递到集合中,如果对象发生更改,集合也应该更改,而无需编写额外的代码。

It seems that string is reference, but copied by value

List<string> data = new List<string>();
string s = "a";
data.Add(s); // copy by value??
s = "b";
Console.WriteLine(data[0]); // "a"

It also seems that class is reference copied by reference

class boxstring { public string val; }
List<boxstring> data = new List<boxstring>();
boxstring s = new boxstring { val = "a" };
data.Add(s); // copy by reference, ok
s.val = "b";
Console.WriteLine(data[0].val); // "b"

Is there a simpler way to pass string into collection by reference without wrapping it into class? I'd like to pass object member into collection and if the object is changed, the collection should be also changed without writing extra code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

橪书 2025-01-14 00:33:47

字符串是不可变的,如果您希望容器中的对象发生更改,它们必须是可变的。因此,您必须像您一样将它们包装在一个类中。

Strings are immutable, if you want the objects in your container to change, they must be mutable. Therefore, you must wrap them in a class like you did.

一抹淡然 2025-01-14 00:33:47

字符串是不可变的。每当您为字符串变量分配新值时,每次都会创建一个新实例,这就是您看不到集合中更新的原因。

然而,.NET 已经提供了字符串的可变对应项,即“StringBuilder”。这对你有用 -

List<StringBuilder> data = new List<StringBuilder>();
StringBuilder s = new StringBuilder("a");
data.Add(s);
s.Clear();
s.Insert(0, "b");
Console.WriteLine(data[0]); // "b"

Strings are immutable. Whenever you assign new value to string variable, a new instance is created everytime that's why you can't see the updates in your collection.

However, .NET already provide mutable counterpart of string i.e. "StringBuilder". This will work for you -

List<StringBuilder> data = new List<StringBuilder>();
StringBuilder s = new StringBuilder("a");
data.Add(s);
s.Clear();
s.Insert(0, "b");
Console.WriteLine(data[0]); // "b"
恏ㄋ傷疤忘ㄋ疼 2025-01-14 00:33:47

这里有一个让你的代码更简单的想法:

public MyString
{
public string Value{get;set;}
public MyString(string value)
{
Value=value;
}



public static implicit operator MyString(string value)
{
return new MyString(value);
}

public static implicit operator string(MyString mystring)
{
if(mystring==null) return null;
return mystring.Value;
}

那么只要你想通过引用获得字符串,你就可以使用 MyString 对象。因为我们有这些隐式运算符,你可以使用 MyString 而不是字符串

Here's an idea to make you code simpler :

public MyString
{
public string Value{get;set;}
public MyString(string value)
{
Value=value;
}



public static implicit operator MyString(string value)
{
return new MyString(value);
}

public static implicit operator string(MyString mystring)
{
if(mystring==null) return null;
return mystring.Value;
}

then you can use MyString object whenever you want to have string by reference.since we have these implicit operator in place you can use MyString instead of string

秋意浓 2025-01-14 00:33:47

您不能通过引用传递内在数据类型,它们始终通过值传递。
内部类型包括 Int32、String、Bool 等基本类型。

You cannot pass intrinsic data-types by reference, they are always passed by value.
Intrinsic types include basic types like Int32, String, Bool, etc..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文