简体   繁体   English

if-evaluation的奇怪行为

[英]Strange behaviour of if-evaluation

I want to check, whether Arguments contains a string from ParameterSwitches. 我想检查Arguments是否包含ParameterSwitches的字符串。 If that is the case, it should be deleted from Arguments. 如果是这种情况,则应从参数中删除它。

The Problem is, even when pSwitch has the same value as Arguments[i], the condition doesn't come true and Arguments[i] = null; 问题是,即使pSwitch与Arguments [i]具有相同的值,条件也不会成立,Arguments [i] = null; won't be executed. 不会被执行。

I've tried it step-by-step with the debugger and just got more confused, because it confirmed my assumption. 我已经尝试了它与调试器一步一步,并且更加困惑,因为它证实了我的假设。

string[] Arguments = new string[]{/*some strings*/};
string[] ParameterSwitches = new string[]{/*some strings*/};
for (int i = 0; i < Arguments.Length; i++)
{
    foreach (string pSwitch in ParameterSwitches)
    {
        if (pSwitch == Arguments[i])
        {
            Arguments[i] = null;
        }
    }
}

Could anyone imagine a way this could happen? 谁能想象出这种情况会发生什么?

Thanks in advance 提前致谢

UPDATE: Oh girls and guys... Next time I use my glasses before asking a question here. 更新:哦,女孩和男人......下次我在问这里之前先用眼镜。 There was a small white space after one of the strings. 其中一根琴弦后面有一个小空格。 That was the problem. 那就是问题所在。 Trimming did the trick. 修剪成功了。

if (String.Equals(pSwitch.Trim().ToUpper(), Arguments[i].Trim().ToUpper()))
{
    Arguments[i] = null;
}

"Problem" solved “问题解决了

try this: 尝试这个:

if(string.Equals(pSwitch.Trim(), Arguments[i].Trim(), StringComparison.OrdinalIgnoreCase))

I assume that your strings don't match because they are not trimmed, or case is wrong, any other case won't match your decription 我假设你的字符串不匹配,因为它们没有被修剪,或者情况有误,任何其他情况都不符合你的描述

For predefined value types, the equality operator (==) returns true if the values of its operands are equal, false otherwise. 对于预定义的值类型,如果操作数的值相等,则相等运算符(==)返回true,否则返回false。 For reference types other than string, == returns true if its two operands refer to the same object. 对于除string之外的引用类型,如果其两个操作数引用同一对象,则==返回true。 For the string type, == compares the values of the strings. 对于字符串类型,==比较字符串的值。

I kinda think this is due to the dereference of Arguments[i] vs the for each loop that put the value of each iteration in a string object. 我认为这是由于Arguments [i]与每个循环的取消引用,它将每个迭代的值放在一个字符串对象中。

What happens if you change the inner loop to imperative code as you did in the outer loop, thus introducing another iteration variable. 如果您像在外部循环中那样将内循环更改为命令式代码会发生什么,从而引入另一个迭代变量。 Or the other way around create two for each loops. 或者反过来为每个循环创建两个。

Mistake in array values, the code works. 错误的数组值,代码工作。

private static void Main(string[] args)
{
    string[] Arguments = new string[] {"111", "222"};
    string[] ParameterSwitches = new string[] {"111", "222"};
    for (int i = 0; i < Arguments.Length; i++)
    {
        foreach (string pSwitch in ParameterSwitches)
        {
            if (pSwitch == Arguments[i])
            {
                // set breakpoint here to see
                Arguments[i] = null;
            }
        }
    }
}

try it 试试吧

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM