C#中的if(a == 5)或if(5 == a)之间有什么区别吗? [重复]

时间:2022-03-13 22:28:56

Just I would like to know, is there any difference between

我想知道,两者之间有什么区别

if (a==5) or if (5==a)

if(a == 5)或if(5 == a)

in C#, Which one is better?

在C#中,哪一个更好?

8 个解决方案

#1


There's no difference - assuming that "a" is an integer.

没有区别 - 假设“a”是一个整数。

I know some people prefer if (5==a) because in c & c++ if you wrote if (5=a) by mistake you'd get a compiler error while if (a=5) would result in a bug.

我知道有些人更喜欢if(5 == a),因为在c&c ++中,如果你错误地写了if(5 = a),你会得到一个编译器错误而if(a = 5)会导致错误。

C# raises a compiler error in the latter case, so it's not an issue.

在后一种情况下,C#会引发编译器错误,所以这不是问题。

#2


I'd actually say there is a difference, but it's not a technical one (as everyone has well covered already) - readability. It matters and the first form is much more natural.

我实际上说有一点不同,但它不是技术性的(因为每个人都已经很好地覆盖了) - 可读性。这很重要,第一种形式更自然。

#3


The if(5 == a) construct is common in C/C++ because boolean values are represented using ints. Thus if you write a = 5 by mistake this can be evaluated in the context of the if, which is most likely not what you wanted.

if(5 == a)构造在C / C ++中很常见,因为布尔值用int表示。因此,如果你错误地写了一个= 5,那么可以在if的上下文中进行评估,这很可能不是你想要的。

In C# there's no implicit conversion from int to bool, so if you type = instead of == you'll get a compile error.

在C#中没有从int到bool的隐式转换,所以如果你键入=而不是==你将得到一个编译错误。

#4


no difference, it is an old habit to avoid if(a=5) in c/c++.

没有区别,如果c / c ++中有(a = 5),这是一个很老的习惯。

These questions/answers are about the same:

这些问题/答案大致相同:

#5


Both are equivalent. I remember when I used to code in C, I preferred 'if (5==a)' because it guarantees that I haven't typed 5=a accidentally as the compiler would throw error. This would not happen if we write 'if (a=5)'. Though it is a typo, it would not generate any compiler error and would go unnoticed.

两者都是等价的。我记得当我以前在C中编码时,我更喜欢'if(5 == a)',因为它保证我没有输入5 = a意外,因为编译器会抛出错误。如果我们写'if(a = 5)'就不会发生这种情况。虽然这是一个拼写错误,但它不会产生任何编译错误,也不会被忽视。

But, in C# it is not the case. There is no logical reason to write 'if (5==a)'. If we had written 'if(a=5)', the compiler would throw an error. So in C# use 'if(a==5)'!

但是,在C#中并非如此。编写'if(5 == a)'没有逻辑上的理由。如果我们写了'if(a = 5)',编译器会抛出错误。所以在C#中使用'if(a == 5)'!

#6


With correct design, there is no difference between "a == 5" and "5 == a". But there is some special situation, where has "a == 5" and "5 == a" different behaviour. It's very unpropably, but it is posible.

通过正确的设计,“a == 5”和“5 == a”之间没有区别。但是有一些特殊的情况,其中有“a == 5”和“5 == a”不同的行为。这是非常不可行的,但它是可行的。

Nevertheless this example is constructed for demonstration of the situation, and I does not recomend do thinks such this.

然而,这个例子是为了证明这种情况而构建的,我不建议这样做。

Example:

public class BadClass {
    public int Value;

    public static implicit operator int( BadClass c ) {
        return c.Value;
    }
    //public static implicit operator BadClass( int n ) {
    //    return new BadClass { Value = n };
    //}

    public static bool operator ==( BadClass c, int n ) {
        return (c.Value + 1 == n);
    }
    public static bool operator !=( BadClass c, int n ) {
        return (c.Value + 1 != n);
    }

    public override bool Equals( object obj ) {
        if ( obj is int ) {
            return (this == (int)obj);
        }
        else {
            return base.Equals( obj );
        }
    }

    public override int GetHashCode() {
        return base.GetHashCode();
    }
}
...
BadClass a = new BadClass { Value = 13 };
var rslt_1 = (13 == a); //there will be true
var rslt_2 = (a == 13); //there will be false

#7


Other than the safety if (5==a) gives you, No.

除了安全性if(5 == a)给你,不。

#8


Only difference is if you forget the second equals, the first version is still a valid statement whereas the second version will raise a compile time error.

唯一不同的是,如果您忘记了第二个等号,则第一个版本仍然是有效语句,而第二个版本将引发编译时错误。

HTH

cheers,

#1


There's no difference - assuming that "a" is an integer.

没有区别 - 假设“a”是一个整数。

I know some people prefer if (5==a) because in c & c++ if you wrote if (5=a) by mistake you'd get a compiler error while if (a=5) would result in a bug.

我知道有些人更喜欢if(5 == a),因为在c&c ++中,如果你错误地写了if(5 = a),你会得到一个编译器错误而if(a = 5)会导致错误。

C# raises a compiler error in the latter case, so it's not an issue.

在后一种情况下,C#会引发编译器错误,所以这不是问题。

#2


I'd actually say there is a difference, but it's not a technical one (as everyone has well covered already) - readability. It matters and the first form is much more natural.

我实际上说有一点不同,但它不是技术性的(因为每个人都已经很好地覆盖了) - 可读性。这很重要,第一种形式更自然。

#3


The if(5 == a) construct is common in C/C++ because boolean values are represented using ints. Thus if you write a = 5 by mistake this can be evaluated in the context of the if, which is most likely not what you wanted.

if(5 == a)构造在C / C ++中很常见,因为布尔值用int表示。因此,如果你错误地写了一个= 5,那么可以在if的上下文中进行评估,这很可能不是你想要的。

In C# there's no implicit conversion from int to bool, so if you type = instead of == you'll get a compile error.

在C#中没有从int到bool的隐式转换,所以如果你键入=而不是==你将得到一个编译错误。

#4


no difference, it is an old habit to avoid if(a=5) in c/c++.

没有区别,如果c / c ++中有(a = 5),这是一个很老的习惯。

These questions/answers are about the same:

这些问题/答案大致相同:

#5


Both are equivalent. I remember when I used to code in C, I preferred 'if (5==a)' because it guarantees that I haven't typed 5=a accidentally as the compiler would throw error. This would not happen if we write 'if (a=5)'. Though it is a typo, it would not generate any compiler error and would go unnoticed.

两者都是等价的。我记得当我以前在C中编码时,我更喜欢'if(5 == a)',因为它保证我没有输入5 = a意外,因为编译器会抛出错误。如果我们写'if(a = 5)'就不会发生这种情况。虽然这是一个拼写错误,但它不会产生任何编译错误,也不会被忽视。

But, in C# it is not the case. There is no logical reason to write 'if (5==a)'. If we had written 'if(a=5)', the compiler would throw an error. So in C# use 'if(a==5)'!

但是,在C#中并非如此。编写'if(5 == a)'没有逻辑上的理由。如果我们写了'if(a = 5)',编译器会抛出错误。所以在C#中使用'if(a == 5)'!

#6


With correct design, there is no difference between "a == 5" and "5 == a". But there is some special situation, where has "a == 5" and "5 == a" different behaviour. It's very unpropably, but it is posible.

通过正确的设计,“a == 5”和“5 == a”之间没有区别。但是有一些特殊的情况,其中有“a == 5”和“5 == a”不同的行为。这是非常不可行的,但它是可行的。

Nevertheless this example is constructed for demonstration of the situation, and I does not recomend do thinks such this.

然而,这个例子是为了证明这种情况而构建的,我不建议这样做。

Example:

public class BadClass {
    public int Value;

    public static implicit operator int( BadClass c ) {
        return c.Value;
    }
    //public static implicit operator BadClass( int n ) {
    //    return new BadClass { Value = n };
    //}

    public static bool operator ==( BadClass c, int n ) {
        return (c.Value + 1 == n);
    }
    public static bool operator !=( BadClass c, int n ) {
        return (c.Value + 1 != n);
    }

    public override bool Equals( object obj ) {
        if ( obj is int ) {
            return (this == (int)obj);
        }
        else {
            return base.Equals( obj );
        }
    }

    public override int GetHashCode() {
        return base.GetHashCode();
    }
}
...
BadClass a = new BadClass { Value = 13 };
var rslt_1 = (13 == a); //there will be true
var rslt_2 = (a == 13); //there will be false

#7


Other than the safety if (5==a) gives you, No.

除了安全性if(5 == a)给你,不。

#8


Only difference is if you forget the second equals, the first version is still a valid statement whereas the second version will raise a compile time error.

唯一不同的是,如果您忘记了第二个等号,则第一个版本仍然是有效语句,而第二个版本将引发编译时错误。

HTH

cheers,