使用未分配的变量(字符串)

发布于 2024-12-19 09:16:51 字数 325 浏览 1 评论 0原文

我有一段迭代 XML 属性的代码:

string groupName;
do
{
    switch (/* ... */)
    {
        case "NAME":
            groupName = thisNavigator.Value;
            break;
        case "HINT":
            // use groupName

但是这样我就得到了使用未分配变量的错误。如果我将某些内容分配给 groupName,则我无法更改它,因为这就是字符串在 C# 中的工作方式。有什么解决方法吗?

I have a piece of code that iterates over XML attributes:

string groupName;
do
{
    switch (/* ... */)
    {
        case "NAME":
            groupName = thisNavigator.Value;
            break;
        case "HINT":
            // use groupName

But this way I get the error of using an unassigned variable. If I assign something to groupName then I cannot change it because that's how the strings work in C#. Any workarounds ?

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

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

发布评论

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

评论(5

恍梦境° 2024-12-26 09:16:51

您认为 .NET 中的字符串是不可变的,这是正确的,但是您认为字符串变量无法更改的假设是错误的。

这是有效且良好的:

string groupName = null;
groupName = "aName";
groupName = "a different Name";

如果执行以下操作,您的代码将不会出现错误:

string groupName = string.Empty; // or null, if empty is meaningful
do
{
    switch (/* ... */)
    {
        case "NAME":
            groupName = thisNavigator.Value;
            break;
        case "HINT":
            // use groupName

You are right that strings are immutable in .NET, but your assumption that a string variable can't be changed is wrong.

This is valid and fine:

string groupName = null;
groupName = "aName";
groupName = "a different Name";

Your code will not have an error if you do the following:

string groupName = string.Empty; // or null, if empty is meaningful
do
{
    switch (/* ... */)
    {
        case "NAME":
            groupName = thisNavigator.Value;
            break;
        case "HINT":
            // use groupName
紫罗兰の梦幻 2024-12-26 09:16:51

您的switchdefault 是否为groupName 分配了值?如果不是,那么就会导致错误。

switch
{
  case "NAME":
    groupName = thisNavigator.Value;
    break;
  //...
  default:
    groupName = "something";
    break;
}

Does the default of your switch assign a value to groupName? If not, then that will be causing the error.

switch
{
  case "NAME":
    groupName = thisNavigator.Value;
    break;
  //...
  default:
    groupName = "something";
    break;
}
空心空情空意 2024-12-26 09:16:51
string groupName = string.Empty;

只需分配一个空字符串,您的应该就可以了。

string groupName = string.Empty;

Just assign an empty string and your should be okej.

念三年u 2024-12-26 09:16:51

编译器不知道 switch 语句的上下文(例如,不能保证 switch 始终与大小写匹配)。

因此,即使在切换之后,groupName 也可能保持未分配状态。

您可以使用 String.Empty 实例化 groupName 或在 switch 语句中使用 default:

The compiler is unaware of the context of your switch statement (e.g. there's no guarantee that the switch will always match a case).

So it's possible for groupName to remain unassigned even after the switch.

You can instantiate groupName with String.Empty or use default: in your switch statement.

安静被遗忘 2024-12-26 09:16:51

在每个 case 中设置 groupName 并在 switch 语句中使用 default 键或将 groupName 分配给 null before switch。

Set groupName in each case and use default key in switch statement or assign groupName to null before switch.

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