Hi Mickey, if you try to overload the conditional logical operators,
following the specs, you will always get the wrong result either in the
logical ops or the bitwise ops. It still seems to me that there is something
wrong in the specs. The only way to make it work is apply logical And in the
bitwise And operator which appears to be logically wrong.
Look at this code:
DbNumber n1 = new DbNumber(1);
DbNumber n2 = new DbNumber(1);
if (n1 && n2)
{
// never gets here because the compiler generated this:
// op_True( op_False(n1) ? n1 : op_BitwiseAnd(n1, n2))
// and it is wrong!
}
if (n1 || n2)
{
// works ok.
}
DbNumber n3 = n1 & n2; // works ok.
DbNumber n4 = n1 | n2; // works ok.
class DbNumber { // it could be struct, it makes no difference
private int m_value;
public DbNumber(int value) {
this.m_value = value;
}
// bitwise and
public static DbNumber operator &(DbNumber n1, DbNumber n2) {
return new DbNumber(n1.m_value & n2.m_value);
}
// bitwise or
public static DbNumber operator |(DbNumber n1, DbNumber n2) {
return new DbNumber(n1.m_value | n2.m_value);
}
// logical operators
public static bool operator true(DbNumber n) {
return n.m_value!=0;
}
public static bool operator false(DbNumber n) {
return n.m_value==0;
}
}
"Mickey Williams" <my first name at servergeek.com> wrote in message
news:OR**************@TK2MSFTNGP10.phx.gbl...
Wow - a real C# language question.
You're misreading the spec and mixing scalar and logical operations.
&& and & are the logical and operators.
&& short-circuits, & does not.
For scalar types, applying & to two instances performs a bitwise
operation. This is not the case for other types.
For scalar types, you cannot apply && or similar operators, as it makes no
sense to short-circuit these operations.
--
Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press
www.servergeek.com/blogs/mickey