Equivalent to Visual Basic And and Or in C #?

I know that AndAlso equivalent to && , and OrElse equivalent to || . But what is the cleanest way to get the equivalent of Visual Basic And and Or in C #?

For example, consider the following VB.NET code. The ValidateForControl method does some checking and returns whether the state of the specified control is valid. The entire input form is valid if all controls are valid. However, each control must be individually tested, even if it is invalid (which requires a short circuit from the operator). The Visual Basic And operator is ideal for this situation, but unfortunately there is no equivalent operator in C # as far as I know ( && short circuits).

 Return _ Me.ValidateForControl(Me.firstNameTextBox) And Me.ValidateForControl(Me.middleNameTextBox) And Me.ValidateForControl(Me.lastNameTextBox) And Me.ValidateForControl(Me.streetAddressTextBox) And Me.ValidateForControl(Me.cityTextBox) And Me.ValidateForControl(Me.stateComboBox) And Me.ValidateForControl(Me.zipCodeMaskedTextBox) And Me.ValidateForControl(Me.phoneMaskedTextBox) And Me.ValidateForControl(Me.emailAddressTextBox) And Me.ValidateForControl(Me.checkInDateTimePicker) And Me.ValidateForControl(Me.checkOutDateTimePicker) And Me.ValidateForControl(Me.rentalUnitsGroupBox) 

Also, for booleans ^ in C # is equivalent to Xor in Visual Basic?

+7
c #
source share
6 answers

And β†’ &

Or β†’ |

Yes, Xor β†’ ^

+7
source share

The MSDN documentation for && contains a sample that shows both "regular AND" & and "short circuit AND" && .

Be sure to comment on the use of & , as most C # programmers will expect && with bool s.


You can also write the And extension method

 static class BooleanExtensions { public static bool And(this bool lhs, bool rhs) { return lhs & rhs; } } 

although you are probably best off sticking with the built-in & syntax. Note that the AndAlso extension method will not be useful, as the rhs argument will be evaluated as part of the method call.

+7
source share

You just use & for And and | for Or . Both ^ and != Xor equivalent to Xor (and to each other) when both operands are Boolean.

+1
source share

Historically (language B, predecessor C), bitwise operators (& |) were used as a logical operator, but since it does not perform a short circuit, they quickly correct the language and introduce short-circuited (& & ||)

In fact, because of this, the priority of bitwise operators still carries the same operator associativity so far. Until now, the bitwise operator (for example, its logical operator) is associated with comparisons, and not in an expression.

Case, this operator associativity ...

 if (A + B == C) 

... do not do it:

 if (A & B == C) 

In the second code, A is evaluated independently, and B == C are evaluated together. Therefore, we need to put brackets for bitwise operators to achieve the same operator associativity as mathematical operations.

 if ( ( A & B ) == C ) 

Languages ​​not supported by C, Java and C # developers could break the tradition and give bitwise operators the same operator associativity as arithmetic operators. But they did not, so a large C-code base can be reused there in these languages.

Before that, I can’t understand why & and | do not behave the same as + - / * .

To answer your question, simply use the bitwise operator:

  protected void Page_Load(object sender, EventArgs e) { bool isValid = A() & B(); Response.Output.Write("<br>Is Valid {0}", isValid); } bool A() { Response.Write("<br>TestA"); return false; } bool B() { Response.Write("<br>TestB"); return true; } 
+1
source share

Use & and | .

0
source share

In this case, you can use the bitwise and operator ( & ):

 return this.ValidateForControl(this.firstNameTextBox) & this.ValidateForControl(this.middleNameTextBox) & this.ValidateForControl(this.lastNameTextBox) & this.ValidateForControl(this.streetAddressTextBox) & this.ValidateForControl(this.cityTextBox) & this.ValidateForControl(this.stateComboBox) & this.ValidateForControl(this.zipCodeMaskedTextBox) & this.ValidateForControl(this.phoneMaskedTextBox) & this.ValidateForControl(this.emailAddressTextBox) & this.ValidateForControl(this.checkInDateTimePicker) & this.ValidateForControl(this.checkOutDateTimePicker) & this.ValidateForControl(this.rentalUnitsGroupBox); 

And yes, xor is written ^ in C # (as in most C-like languages).

0
source share

All Articles