Example #1
0
        private static void OptionalInterfaceDemo()
        {
            Console.WriteLine("\n*** Optional Interface Demo:");

            Person       jill     = new Person("Jill", 19, Color.Red); // Has a car
            Option <Car> jillsCar = jill.TryGetCar();                  // Some

            if (jillsCar is Some <Car> some1)
            {
                PrintName(some1);
            }

            Option <ICar> abstractCar =
                jill.TryGetCar().OfType <ICar>();  // Some<ICar>

            if (abstractCar is Some <ICar> someOther)
            {
                PrintAbstractName(someOther.Content);
            }
        }
Example #2
0
        private static void MappingDemo()
        {
            Console.WriteLine("\n*** Mapping demo:");

            Person child   = new Person("Jill", 12, Color.Red);
            Person grownUp = new Person("Joe", 46, Color.Blue);

            Option <Car> none = child.TryGetCar();  // None
            Option <Car> some = grownUp.TryGetCar();

            Console.WriteLine($"{none}, {some}");

            Option <Person> noPerson    = None.Value;
            Option <Person> someChild   = child;
            Option <Person> someGrownUp = grownUp;

            Option <Car> noCar          = noPerson.MapOptional(person => person.TryGetCar());
            Option <Car> noChildCar     = someChild.MapOptional(person => person.TryGetCar());
            Option <Car> someGrownUpCar = someGrownUp.MapOptional(person => person.TryGetCar());

            Console.WriteLine($"{noCar}, {noChildCar}, {someGrownUpCar}");
        }