Hi. I have a question about the evaluation of IF statements when the &&
operator is used. I have been under the assumption that if the first
expression in an IF statement is FALSE it will not evaluate anything after
the && in the same statement.
I have some code that is similar to this:
PaymentEntity pe = GetPaymentEntity();
if (pe.Type == PaymentType.CreditCard &&
((CreditCardPayment)pe).CreditCard.Number > 0)
{
...
}
PaymentEntity - Custom object representing a payment
PaymentType - Custom enum listing the multiple payment types we accept
CreditCardPayment - Custom object representing a credit card payment
(extends PaymentEntity)
I am having a problem where I get an error that states "Specified cast not
valid" when the code is executed with a payment type other than
PaymentType.CreditCard. It appears that an attempt is made to cast pe as a
CreditCardPayment.
My question is, why is the cast even attempted when the first part of the IF
statement evaluates to false?
Thanks.