Exemple #1
0
        static void Main(string[] args)
        {
            #region Assessment A

            // the below class declarations looks like a 1st year student developed it
            // NOTE: this includes the class declarations as well
            // IMPROVE THE ARCHITECTURE
            var human = new Human
            {
                Name   = "John",
                Age    = 35,
                Gender = "M"
            };
            WriteLine(human.GetDetails());

            var dog = new Dog
            {
                Name = "Walter",
                Age  = 7,
                Food = "Epol"
            };
            WriteLine(dog.GetDetails());

            var cat = new Cat
            {
                Name = "Snowball",
                Age  = 35,
                Food = "Whiskers"
            };
            WriteLine(cat.GetDetails());

            //PerformanceTest();

            //Created list of integers
            List <int> numbers = new List <int>()
            {
                1, 4, 5, 9, 11, 15, 20, 27, 34, 55 // you may not change the numbers
            };
            // the following method must return the first event number - as suggested by it's name
            var firstValue = GetFirstEvenValue(numbers);
            WriteLine("First Number: " + firstValue);

            List <string> strings = new List <string>()
            {
                "John", "Jane", "Sarah", "Pete", "Anna"
            };
            // the following method must return the first name which contains an 'a'
            var strValue = GetSingleStringValue(strings);
            WriteLine("Single String: " + strValue);

            #endregion

            #region Assessment B

            // you'll notice the following piece of code takes an
            // age to execute - CORRECT THIS
            // IT MUST EXECUTE IN UNDER A SECOND
            //PerformanceTest();

            #endregion

            #region Assessment C


            // correct the following LINQ statement found in their respective methods
            //List<int> numbers = new List<int>()
            //{
            //    1, 4, 5, 9, 11, 15, 20, 27, 34, 55 // you may not change the numbers
            //};
            //// the following method must return the first event number - as suggested by it's name
            //var firstValue = GetFirstEvenValue(numbers);
            //WriteLine("First Number: " + firstValue);

            //List<string> strings = new List<string>()
            //{
            //    "John", "Jane", "Sarah", "Pete", "Anna"
            //};
            //// the following method must return the first name which contains an 'a'
            //var strValue = GetSingleStringValue(strings);
            //WriteLine("Single String: " + strValue);

            #endregion



            #region Assessment D

            // there are multiple corrections required!!
            // correct the following statement(s)
            try
            {
                //object an instance of a class
                Dog bulldog    = null;
                var disposeDog = (IDisposable)bulldog;
                disposeDog.Dispose();
            }
            catch (Exception ex)
            {
                WriteLine(ex.Message);
            }

            #endregion

            #region Assessment E

            DisposeSomeObject();

            #endregion

            #region Assessment F

            // # SECTION A #
            // by making use of generics improve the implementation of the following methods
            // output must still render as: Name: [name] Age: [age]
            // THE METHOD THAT YOU CREATE MUST BE STATIC AND DECLARED IN THE PROGRAM CLASS
            // NB!! PLEASE NAME THE METHOD: ShowSomeMammalInformation
            ShowSomeHumanInformation(human);
            ShowSomeDogInformation(dog);
            ShowSomeCatInformation(cat);
            ShowSomeMammalInformation(human, dog, cat);

            // # SECTION B #
            // BY MAKING USE OF REFLECTION (amongst other things):
            //      => create a method so that the below code snippet will work:
            //      => place a constraint on the new method, so that a new instance will be created when 'dog' is null
            //      => thus is dog = null, the method should create a new instance an not fail

            // UNCOMMENT THE FOLLOWING PIECE OF CODE - IT WILL CAUSE A COMPILER ERROR - BECAUSE YOU HAVE TO CREATE THE METHOD

            //string a = Program.GenericTester(walter => walter.GetDetails(), dog);
            //WriteLine("Result A: {0}", a);
            //int b = Program.GenericTester(snowball => snowball.Age, cat);
            //WriteLine("Result B: {0}", b);

            #endregion

            #region Assessment G

            // in the following statement, everything works fine
            // but, it has a huge flaw!
            // correct the following piece of code
            try
            {
                CatchAndRethrowExplicitly();
            }
            catch (ArithmeticException e)
            {
                WriteLine("Implicitly specified:{0}{1}", Environment.NewLine, e.StackTrace);
            }

            #endregion

            #region Assessment H

            try
            {
                // REFLECTION TEST .... NAVIGATE TO THE BELOW METHOD TO GET ALL THE INSTRUCTIONS
                CallMethodWithReflection();
            }
            catch (Exception e)
            {
                WriteLine(e.Message);
            }

            #endregion

            #region IoC / DI

            // everything can be viewed in this method....
            PerformIoCActions();

            #endregion

            #region Bonus XP - Dungeon

            // > UNCOMMENT THE CODE BELOW AND CREATE A METHOD SO THAT THE FOLLOWING CODE WILL WORK
            // > DECLARE ALL THE METHODS WITHIN THE PROGRAM CLASS !!
            // > DO NOT ALTER THE EXISTING CODE

            /*
             *  const string abc = "asduqwezxc";
             *  foreach (var vowel in abc.SelectOnlyVowels())
             *  {
             *      WriteLine("{0}", vowel);
             *  }
             */
            // < REQUIRED OUTPUT => a u e

            // > UNCOMMENT THE CODE BELOW AND CREATE A METHOD SO THAT THE FOLLOWING CODE WILL WORK
            // > DECLARE ALL THE METHODS WITHIN THE PROGRAM CLASS !!
            // > DO NOT ALTER THE EXISTING CODE

            /*
             * List<Dog> dogs = new List<Dog>
             * {
             *  new Dog {Age = 8, Name = "Max"},
             *  new Dog {Age = 3, Name = "Rocky"},
             *  new Dog {Age = 9, Name = "XML"}
             * };
             *
             * var foo = dogs.CustomWhere(x => x.Age > 6 && x.Name.SelectOnlyVowels().Any());
             *
             * // < DOGS REQUIRED OUTPUT =>
             * //      Name: Max Age: 8
             *
             * List<Cat> cats = new List<Cat>
             * {
             *  new Cat {Age = 1, Name = "Capri"},
             *  new Cat {Age = 8, Name = "Cara"},
             *  new Cat {Age = 3, Name = "Captain Hooks"}
             * };
             *
             * var bar = cats.CustomWhere(x => x.Age <= 4);
             * // < CATS REQUIRED OUTPUT =>
             * //      Name: Capri Age: 1
             * //      Name: Captain Hooks Age: 3
             */

            #endregion

            WriteLine("Press any key to continue...");
            ReadLine();
        }
 private static string GenericTester(Func <object, object> p, Dog mammal)
 {
     return(mammal.GetDetails());
 }