3

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

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

in C#, Which one is better?

Dhanapal
  • 14,239
  • 35
  • 115
  • 142

8 Answers8

19

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

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.

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

ChrisF
  • 134,786
  • 31
  • 255
  • 325
9

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.

annakata
  • 74,572
  • 17
  • 113
  • 180
7

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.

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

Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
5

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

These questions/answers are about the same:

Community
  • 1
  • 1
eglasius
  • 35,831
  • 5
  • 65
  • 110
3

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.

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)'!

softwarematter
  • 28,015
  • 64
  • 169
  • 263
1

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.

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
TcKs
  • 25,849
  • 11
  • 66
  • 104
0

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

Pondidum
  • 11,457
  • 8
  • 50
  • 69
  • 2
    In C# there's no extra safety cause if (a=5) isn't allowed – Brian Rasmussen Apr 28 '09 at 10:12
  • @Brian: "if (a=5)" can be allowed, if "a" is some custom type which can be implicitly converted from int and implicitly converted to bool. – TcKs Apr 28 '09 at 10:30
  • @TcKs: Correct but you don't see those very often do you? For bools you can make the assignment instead of comparison mistake if explicitly comparing with true, which is another good reason to avoid if (b == true). – Brian Rasmussen Apr 28 '09 at 10:38
0

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,

Rob Wells
  • 36,220
  • 13
  • 81
  • 146