如何在 C# 中获取字符串属性的引用

发布于 2024-10-18 21:22:11 字数 568 浏览 4 评论 0原文

假设我有一个具有三个字符串属性的类:

public class Foo
{
  public string Bar1 { get; set; }
  public string Bar2 { get; set; }
  public string Bar3 { get; set; }
}

现在假设我想分配给其中一个字符串属性,但是我分配给三个属性中的哪一个取决于某些条件。知道字符串应该是引用类型,我可能会想编写一些如下代码:

string someString;
if (condition1) someString = foo.Bar1;
else if (condition2) someString = foo.Bar2;
else if (condition3) someString = foo.Bar3;
someString = "I can't do that, Dave.";

这不起作用。我知道这与字符串不变性有关(至少我认为确实如此),但我不知道该怎么做。

弦乐基本上让我感到困惑。

嗯,是的,所以我的问题是最简洁的方法是什么?

Say I have a class with three string properties:

public class Foo
{
  public string Bar1 { get; set; }
  public string Bar2 { get; set; }
  public string Bar3 { get; set; }
}

Now say I want to assign to one of the string properties, but that which of the three properties I assign to depends upon some condition. Knowing that strings are supposedly reference types, I might be tempted to write some code like this:

string someString;
if (condition1) someString = foo.Bar1;
else if (condition2) someString = foo.Bar2;
else if (condition3) someString = foo.Bar3;
someString = "I can't do that, Dave.";

This doesn't work. I know it's got something to do with string immutability (at least I think it does) but I haven't any idea how to do it.

Strings basically confuse the bejesus out of me.

Um, yeah, so my question is what's the most concise way to do this?

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

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

发布评论

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

评论(3

深海蓝天 2024-10-25 21:22:11

就我个人而言,我可能会继续分配属性:

string value = "I can't do that, Dave.";
if (condition1) foo.Bar1 = value;
else if (condition2) foo.Bar2 = value;
else if (condition3) foo.Bar3 = value;

如果您真的想要使用您建议的方法,您可以将其包装在委托中,我猜:

Action<string> assignString;
if (condition1) assignString = s => foo.Bar1 = s;
else if (condition2) assignString = s => foo.Bar2 = s;
else if (condition3) assignString = s => foo.Bar3 = s;
assignString("I can't do that, Dave.");

......但在这种情况下,这只会让事情变得不必要的复杂。对于问题中描述的场景,我想不出您想要这样做的任何原因。

Personally I would probably just go ahead and assign the property:

string value = "I can't do that, Dave.";
if (condition1) foo.Bar1 = value;
else if (condition2) foo.Bar2 = value;
else if (condition3) foo.Bar3 = value;

If you really want to use the approach you suggest, you can wrap it in a delegate I guess:

Action<string> assignString;
if (condition1) assignString = s => foo.Bar1 = s;
else if (condition2) assignString = s => foo.Bar2 = s;
else if (condition3) assignString = s => foo.Bar3 = s;
assignString("I can't do that, Dave.");

...but in this case that would only make things unnecessarily complex. For the kind of scenario that is described in the question, I can't think of any reason you would want to do this.

冬天旳寂寞 2024-10-25 21:22:11

就这样做:

string someString = "I can't do that, Dave.";
if (condition1) foo.Bar1 = someString;
else if (condition2) foo.Bar2 = someString;
else if (condition3) foo.Bar3 = someString;

C# 尝试使字符串尽可能易于使用。它们是原始类型,因此您实际上不需要担心可变性、内存、地址或类似的问题。

Just do it like this:

string someString = "I can't do that, Dave.";
if (condition1) foo.Bar1 = someString;
else if (condition2) foo.Bar2 = someString;
else if (condition3) foo.Bar3 = someString;

C# tries to make strings as easy as possible to work with. They are primitive types, so you don't really need to worry about mutability or memory or addresses or anything like that.

稀香 2024-10-25 21:22:11

您始终可以这样做:

EDITED **

var someString = (condition1) ? foo.Bar1 : (条件2) ? foo.Bar2 : (条件3) ? foo.Bar3:“戴夫,我做不到”;

让我知道你进展如何。

You can always do it like this:

EDITED **

var someString = (condition1) ? foo.Bar1 : (condition2) ? foo.Bar2 : (condition3) ? foo.Bar3 : "I can't do that Dave";

Let me know how you go.

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