As others have noted, this is a defensive tactic against problems that might arise because in C and some C derivatives (including C #), an assignment expression evaluates the value of this assignment. This allows you to:
if (a = true) { /* This will always get done, as "a = true" evals to true */ }
and
int a = b = c = d = 10;
Since assignment is the correct associative, it is effective
int a = (b = (c = (d = 10)));
where each expression inside a pair of curly braces will evaluate the assignment value, which in this case is 10 and a, b, c and d will thus be 10.
To avoid possible mistakes - mixing assignment and equality operators - some programmers prefer to always put a constant on the left, as if the assignment operator was accidentally used, the compiler will complain that you cannot assign a constant.
This, however, has fewer problems in C # for two reasons. Firstly, unlike C, C # does not allow interpreting arbitrary values โโas a logical value.
This was necessary in C because it did not have a true boolean type, it simply relied on interpreting other values, such as integers (where 0 is false and not zero is true) or pointers (where NULL is false). That meant you could do something like
if (10) { } if (0) { } if (p) { } if (NULL) { }
However, since C # does not allow arbitrary expressions to be interpreted as logical, they will not work in C #. It also means that
if (a = 10) { }
will not compile in C #, since the expression "a = 10" evaluates to the value of expression 10, which then cannot be interpreted as the required logical value.
The second reason this is less is that in a much lower percentage of cases where the result of an assignment can be interpreted as a boolean, the compiler issues a warning to make sure you really mean it.
Warning can be suppressed with
#pragma warning disable 665
However, the presence of such code is often a bad smell of code, and refactoring is probably best to make the code more understandable.