Visual Basic的和等价的还是在C#? [英] Equivalent of Visual Basic's And and Or in C#?

查看:183
本文介绍了Visual Basic的和等价的还是在C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 AndAlso运算等同于&放大器;&安培; OrElse运算等同于 || 。但是的什么是实现等效的最彻底的方法的的Visual Basic的或者中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#?

例如,请考虑以下VB.NET code。在 ValidateForControl 方法执行一些验证并返回指定控件的状态是否有效。如果所有的控制是有效的整个输入表单有效。然而,每个控制必须单独验证即使一个是无效的(即要求操作者不短路)。 Visual Basic的运营商非常适合这种情况,但不幸的是,据我所知(&放有一个在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)

此外,对于布尔值,是 ^ 在C#中等同于异或在Visual Basic?

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

推荐答案

- > &安培;

或者 - > |

异或 - > ^

这篇关于Visual Basic的和等价的还是在C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆