operator overloading true and false

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • xllx.relient.xllx@gmail.com

    #1

    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.WriteLi ne("In true operator");
    return true;
    }

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

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

    if ( A )
    Console.WriteLi ne("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.

  • Rudderius

    #2
    Re: operator overloading true and false

    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.WriteLi ne("In true operator : return true");
    return true;
    }
    else
    {
    Console.WriteLi ne("In true operator: return false");
    return false;
    }
    }

    xllx.relient.xl lx@gmail.com wrote:[color=blue]
    > 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.WriteLi ne("In true operator");
    > return true;
    > }
    >
    > public static bool operator false( AnimalClass lhs )
    > {
    > Console.WriteLi ne("In false operator");
    > return false;
    > }
    > }
    >
    > public class MainClass
    > {
    > public static void Main( )
    > {
    > AnimalClass A = null;
    >
    > if ( A )
    > Console.WriteLi ne("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.
    >[/color]

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: operator overloading true and false

      <xllx.relient.x llx@gmail.com> wrote:[color=blue]
      > 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:[/color]

      <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>
      [color=blue]
      > 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.[/color]

      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 - <skeet@pobox.co m>
      http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
      If replying to the group, please do not mail me too

      Comment

      Working...