471,570 Members | 1,680 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,570 software developers and data experts.

operator overloading true and false

Hi, there. I need two simple things explained to me:

1.) I want to know why the "true" overloaded operator method gets
called with a test to a null-initialized instance:

public class AnimalClass
{
public static bool operator true( AnimalClass lhs )
{
Console.WriteLine("In true operator");
return true;
}

public static bool operator false( AnimalClass lhs )
{
Console.WriteLine("In false operator");
return false;
}
}

public class MainClass
{
public static void Main( )
{
AnimalClass A = null;

if ( A )
Console.WriteLine("true");
}
}

// output: In true operator
// is true

2.) How can you call the "false" operator method? because I've tried to
in many ways and I cannot get it to be invoked - only "true" works.

Thanks,
relient.

Jan 15 '06 #1
2 1835
I think this is because the overloaded operator is a static method, so
it needs no instance to work. secondly you do not use the parameter lhs
for this operator. Compare it to calling a static member return a fixed
value like
public static int GetInt()
{
return 0;
}

as far as i know, this must solve your problem:
public static bool operator true( AnimalClass lhs)
{
if (lhs != null)
{
Console.WriteLine("In true operator : return true");
return true;
}
else
{
Console.WriteLine("In true operator: return false");
return false;
}
}

xl***************@gmail.com wrote:
Hi, there. I need two simple things explained to me:

1.) I want to know why the "true" overloaded operator method gets
called with a test to a null-initialized instance:

public class AnimalClass
{
public static bool operator true( AnimalClass lhs )
{
Console.WriteLine("In true operator");
return true;
}

public static bool operator false( AnimalClass lhs )
{
Console.WriteLine("In false operator");
return false;
}
}

public class MainClass
{
public static void Main( )
{
AnimalClass A = null;

if ( A )
Console.WriteLine("true");
}
}

// output: In true operator
// is true

2.) How can you call the "false" operator method? because I've tried to
in many ways and I cannot get it to be invoked - only "true" works.

Thanks,
relient.

Jan 15 '06 #2
<xl***************@gmail.com> wrote:
Hi, there. I need two simple things explained to me:

1.) I want to know why the "true" overloaded operator method gets
called with a test to a null-initialized instance:
<snip>

The compiler sees an AnimalClass expression which needs converting to
true/false, so it calls the true operator with the value of the
expression - which is null in this case.

<snip>
2.) How can you call the "false" operator method? because I've tried to
in many ways and I cannot get it to be invoked - only "true" works.


Hmm... looking at the spec, I can't see any examples of where it's
called. Odd.

I must say, I've *never* seen these operators used in production code -
I would suggest you steer clear of them providing an explicit method
call so that it's more obvious what's going on.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 16 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.