true and false operator : True false operator « Operator Overload « C# / CSharp Tutorial






using System;

    public class MyType
    {
        public static bool operator true ( MyType e )
        {
            return  ( e == null ) ? false : e.b;
        }

        public static bool operator false ( MyType e )
        {
            return  ( e == null ) ? true : !e.b;
        }

        public bool b;

        public MyType( bool b )
        {
            this.b = b;
        }

        public static void Main( string[] args )
        {
            MyType myTrue = new MyType( true );
            MyType myFalse = new MyType( false );
            MyType myNull = null;

            if ( myTrue )
            {
                System.Console.WriteLine( "true" );
            }
            else
            {
                System.Console.WriteLine( "false" );
            }

            if ( myFalse )
            {
                System.Console.WriteLine( "true" );
            }
            else
            {
                System.Console.WriteLine( "false" );
            }

            if ( myNull )
            {
                System.Console.WriteLine( "true" );
            }
            else
            {
                System.Console.WriteLine( "false" );
            }
        }
    }
true
false
false








8.13.True false operator
8.13.1.true/false operator for Complex
8.13.2.Overload true and false for TwoDimension.
8.13.3.true and false operator