Mega Code Archive

 
Categories / C# Tutorial / Operator Overload
 

True and false operator

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