Пример #1
0
        private static void GetPopulationUsingVar(IOrganism organism)
        {
            double population = 0;

            // Switch on passed organism using var pattern matching.
            switch (organism)
            {
            // Assign organism to new bee variable, if population is roughly equal to 30 trillion.
            case var bee when Math.Abs(bee.Population - 30e12) <= 1:
                population = bee.Population;

                break;

            // Assign organism to new human variable, if object type Name is "Human."
            case var human when human.GetType().Name == "Human":
                population = human.Population;

                break;

            default:
                // Output alert if organism type is unknown.
                Logging.Log($"Unknown organism type ({organism.GetType().Name}), or population exceeds all known estimates.");
                return;
            }
            // Output retrieved population estimate.
            Logging.Log($"Estimated number of {organism.GetType().Name.ToLower()}s on Earth: {population:n0}.");
        }
Пример #2
0
        private static void GetPopulationUsingType(IOrganism organism)
        {
            double population = 0;

            // Switch on passed organism using type pattern matching.
            switch (organism)
            {
            case Bee bee:
                population = bee.Population;
                break;

            case Human human when(human.Population < 1e7):
                // If a Human is passed and the population is too low, panic!
                Logging.Log($"The human population is too low at {human.Population:n0}!  Apocalypse!");

                return;

            case Human human:
                // If the Human population is alright, proceed as normal.
                population = human.Population;
                break;

            case Insect insect:
                population = insect.Population;
                break;

            case Mammal mammal:
                population = mammal.Population;
                break;

            default:
                // Output alert if organism type is unknown.
                Logging.Log($"Unknown organism type ({organism.GetType().Name}), or population exceeds all known estimates.");
                return;
            }
            // Output retrieved population estimate.
            Logging.Log($"Estimated number of {organism.GetType().Name.ToLower()}s on Earth: {population:n0}.");
        }