English 中文(简体)
C# bool expression evaluation order [duplicate]
原标题:
This question already has answers here:
Closed 13 years ago.

Possible Duplicate:
== Operator and operands

Possible Duplicates:
Is there any difference between if(a==5) or if(5==a) in C#?
== Operator and operands

Ok, this may be stupid question, but searching in google (cant seem to ACTUALLY search for an exact phrase even with quotes)

What if any difference is there between

if(false == <somecondition>){

and

if(<somecondition> == false){

in C#? I am familiar with c# s expression evalution, and understand how the order would make sense if you were doing something like:

if(AccountIsInvalid || AccountIsUnregistered)

I dont need a huge lesson, but would like to understand why some people prefer to do things with false==.... route.

问题回答

Personally, I never compare anything to false or true.

I would go with:

if (!somecondition) 

or:

if (somecondition)

In C there would have been, perhaps, some reason to do this as you could easily make a mistake and use the assignment operator instead of the comparison operator, but in C# it shouldn t make any difference -- you ll get a compile warning if you use the assignment operator. If the assignment were a different type (say int), it would result in an error, since the result wouldn t be a legal expression for the if statement.

I would prefer it to be

if (!<somecondition>)
{
  ...
}

rather than a comparison to false (or true, for that matter).

Before performing any optimizations or short-circuits, the compiler needs to resolve <somecondition> to true or false value, thus there is no reason why the compiler would evaluate the expressions <somecondition> == false and false == <somecondition> any differently.

This must surely be an issue of style, and style only.

This doesn t matter in C# like it does in c/c++ because conditions must evaluate to a boolean.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签