简体   繁体   中英

Equivalent of Visual Basic's And and Or in C#?

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

For example, consider the following VB.NET code. The ValidateForControl method performs some validation 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 validated even if one is invalid (which requires the operator not to short-circuit). Visual Basic's And operator is perfect for this situation, but unfortunately there's 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, is ^ in C# equivalent to Xor in Visual Basic?

And --> &

Or --> |

Yes, Xor --> ^

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

Be sure to comment your use of & as most C# programmers will be expecting && with bool s.


You could also write an And extension method

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

although you're probably better off just sticking with the native & syntax. Note that an AndAlso extension method wouldn't be useful as the rhs argument would get evaluated as part of the method call.

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

Historically (B language, precursor to C), bitwise operators(& |) was used as logical operator, but since it doesn't do short circuiting, they quickly rectify the language and introduced short-circuiting ones (&& ||)

In fact, because of it, the bitwise-operators precedence still carry the same operator associativity up to now. That up to now, the bitwise operator(like it's logical operator) bind to comparisons instead of in expression.

Case in point, this operator associativity ...

if (A + B == C)

...is not followed by this:

if (A & B == C)

On the second code, A is evaluated independently and B == C are evaluated together. That's why we need to put parenthesis for bitwise-operators to achieve the same operator associativity as mathematical operations.

if ( ( A & B ) == C )

C-inspired languages were not spared, designers of Java and C# could break the tradition and give the bitwise operators the same operator associativity as arithmetic operators. But they didn't, so the large C codebase out there can be re-used on those languages.

Before, I cannot grasp why & and | doesn't behave as same as + - / * .

To answer your question, just use 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;
    }

Use & and | .

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 spelled ^ in C# (as in most C-like languages).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM