public string Display()
        {
            // IEnumerable only allows the given object to be iterated (usually with a foreach)
            var displayText = "";

            IList <int> dummyData = new List <int>()
            {
                0, 6, 7, 1, 8, 9, 4, 7, 7, 1, 5, 8, 2
            };

            displayText += DisplayFormatHelpers.DescriptionValueFormat(
                "Initial List",
                DisplayFormatHelpers.WriteList(dummyData)
                );

            var whereResults = Where <int>(dummyData, (i) => i >= 5);

            displayText += DisplayFormatHelpers.DescriptionValueFormat(
                "Using the Where<T>() method to keep values which are >= 5 only ||  Where<int>(dummyData, (i) => i >= 5 );",
                DisplayFormatHelpers.WriteList(whereResults)
                );

            var whereExtensionResult = dummyData.Where((i) => i >= 5);

            displayText += DisplayFormatHelpers.DescriptionValueFormat(
                "Using the .Where() extension to achieve the same result ||  dummyData.Where((i) => i >= 5);",
                DisplayFormatHelpers.WriteList(whereExtensionResult)
                );



            return(displayText);
        }
        public string Display()
        {
            string consoleText = "";

            var list = new List <object>
            {
                new Something("val one", "val two"),
                2,
                false,
                3.2,
                3.2f,
                3.2m,
                's',
                "something",
                new StructExaple("struct val one", "struct val two")
            };
            IEnumerable <object> items = new EnumerableExample <object>(list);


            consoleText += DisplayFormatHelpers.DescriptionValueFormat
                           (
                "Raw list: ",
                DisplayFormatHelpers.WriteList(list)
                           );

            consoleText += DisplayFormatHelpers.DescriptionValueFormat
                           (
                "Printing the above list using the custom .ToString(): ",
                items
                           );



            return(consoleText);
        }
Exemple #3
0
        public string Display()
        {
            var consoleText = "";

            int num = 42;

            consoleText += DisplayFormatHelpers.DescriptionValueFormat("Integer before using the extension method AddOne()", num.ToString());

            num          = num.AddOne();
            consoleText += DisplayFormatHelpers.DescriptionValueFormat("Integer after using the extension method AddOne()", num.ToString());

            return(consoleText);
        }
Exemple #4
0
        public string Display()
        {
            var displayText = "";

            IList <Car> carList = new Car[]
            {
                new Car()
                {
                    Brand = "Toyota", Make = "Prius"
                },
                new Car()
                {
                    Brand = "Subaru", Make = "Impreza"
                },
                new Car()
                {
                    Brand = "Mitsubishi", Make = "Lancer"
                },
                new Car()
                {
                    Brand = "Skoda", Make = "Octavia"
                },
                new Car()
                {
                    Brand = "VW", Make = "Golf"
                },
                new Car()
                {
                    Brand = "Nissan", Make = "GTR"
                }
            };

            displayText += DisplayFormatHelpers.DescriptionValueFormat
                           (
                "Raw object (using fields)",
                DisplayFormatHelpers.WriteList(carList)
                           );

            var convertor = new ObjectListToCsv <Car>(carList);

            displayText += DisplayFormatHelpers.DescriptionValueFormat
                           (
                "Object list to CSV format",
                convertor.ToCsv()
                           );

            return(displayText);
        }
        public string Display()
        {
            // IEnumerable only allows the given object to be iterated (usually with a foreach)
            var displayText = "";

            IList <string> listExample = new List <string>()
            {
                "one", "two", "three", "four"
            };

            displayText += DisplayFormatHelpers.DescriptionValueFormat
                           (
                "Initial list which implements the IList interface",
                DisplayFormatHelpers.WriteList <string>(listExample)
                           );

            Console.WriteLine(listExample[2]);
            displayText += DisplayFormatHelpers.DescriptionValueFormat
                           (
                "Access item by index, [2]",
                listExample[2]
                           );
            //listExmaple.IndexOf("two");
            Console.WriteLine(listExample[2]);
            displayText += DisplayFormatHelpers.DescriptionValueFormat
                           (
                "Get index of a specific item, \"three\"",
                listExample.IndexOf("three")
                           );

            //listExmaple.Insert(1, "inserted item");
            listExample.Insert(2, "inserted");
            displayText += DisplayFormatHelpers.DescriptionValueFormat
                           (
                "Insert item at a specific index, \"inserted\" at [2]",
                DisplayFormatHelpers.WriteList <string>(listExample)
                           );

            //listExmaple.RemoveAt(3);
            listExample.RemoveAt(3);
            displayText += DisplayFormatHelpers.DescriptionValueFormat
                           (
                "Remove the item at index [3]",
                DisplayFormatHelpers.WriteList <string>(listExample)
                           );

            return(displayText);
        }
Exemple #6
0
        public string Display()
        {
            string consoleText = "";

            int    i        = 42;
            object boxedI   = i;
            int    unboxedI = (int)boxedI;



            try
            {
                string wrongFormatToUnbox = (string)boxedI;
            }
            catch (InvalidCastException ex)
            {
                consoleText += DisplayFormatHelpers.DescriptionValueFormat("Trying to unbox with a wrong type", ex.Message);
            }

            return(consoleText);
        }
Exemple #7
0
        public string Display()
        {
            string consoleText = "";

            Example exampleInstance = new Example();

            IOne exampleIOne = exampleInstance;
            ITwo exampleITwo = exampleInstance;

            consoleText += DisplayFormatHelpers.DescriptionValueFormat(
                "Accessing the method explicitly using the type of the variable",
                exampleIOne.SaySomething()
                );


            consoleText += DisplayFormatHelpers.DescriptionValueFormat(
                "Accessing the method explicitly by type casting the class itself",
                ((ITwo)exampleInstance).SaySomething()
                );

            return(consoleText);
        }
Exemple #8
0
        public string Display()
        {
            // IEnumerable only allows the given object to be iterated (usually with a foreach)
            var displayText = "";

            IEnumerable enumClass = new RandomNumbers(1, 10, 5);
            List <int>  randNums  = new List <int>(5);

            foreach (int randNum in enumClass)
            {
                randNums.Add(randNum);
            }
            displayText += DisplayFormatHelpers.DescriptionValueFormat
                           (
                "Iterating over the custom iterable class using a foreach",
                DisplayFormatHelpers.WriteList(randNums)
                           );

            Console.WriteLine("\n\n\n");
            IEnumerable enumClass2 = new RandomNumbers(1, 3, 10);

            List <int> randNums2 = new List <int>(5);

            IEnumerator enumClass2Enumerator = enumClass2.GetEnumerator();

            while (enumClass2Enumerator.MoveNext())
            {
                randNums2.Add((int)enumClass2Enumerator.Current);
            }
            displayText += DisplayFormatHelpers.DescriptionValueFormat
                           (
                "Iterating over the custom iterable class using a while loop",
                DisplayFormatHelpers.WriteList(randNums2)
                           );

            return(displayText);
        }
        public string Display()
        {
            var displayText = "";

            var someList = new List <string> {
                "one", "two", "three", "four", "five"
            };

            displayText += DisplayFormatHelpers.DescriptionValueFormat
                           (
                "List, type: " + someList.GetType().ToString(),
                DisplayFormatHelpers.WriteList(someList)
                           );

            string[] someListArray = someList.ToArray();

            displayText += DisplayFormatHelpers.DescriptionValueFormat
                           (
                "List to array, type: " + someListArray.GetType().ToString(),
                DisplayFormatHelpers.WriteList <string>(someListArray)
                           );

            return(displayText);
        }
Exemple #10
0
        public string Display()
        {
            // dummy data

            // where, select, any, take, skip, first, single
            string consoleText = "";


            var dummyData = new List <Person>()
            {
                new Person {
                    FirstName = "Johnny", LastName = "Bravo", Gender = "M", Age = 20
                },
                new Person {
                    FirstName = "Johnny", LastName = "Smith", Gender = "M", Age = 25
                },
                new Person {
                    FirstName = "Delia", LastName = "Tonkes", Gender = "F", Age = 30
                },
                new Person {
                    FirstName = "Hunter", LastName = "Guntrip", Gender = "M", Age = 19
                },
                new Person {
                    FirstName = "Fayina", LastName = "Dungate", Gender = "F", Age = 24
                },
                new Person {
                    FirstName = "Lola", LastName = "Porker", Gender = "F", Age = 35
                },
                new Person {
                    FirstName = "Dimitri", LastName = "Winsor", Gender = "M", Age = 40
                },
                new Person {
                    FirstName = "Dimitri", LastName = "Winsor", Gender = "M", Age = 45
                },
                new Person {
                    FirstName = "Dimitri", LastName = "Winsor", Gender = "M", Age = 25
                }
            };

            consoleText += descriptionValueFormat("Raw dummy data", DisplayFormatHelpers.WriteList <Person>(dummyData));


            // where
            var femaleOnly = dummyData.Where(person => person.Gender == "F");

            consoleText += descriptionValueFormat("where method, select only Gender = \"F\"", DisplayFormatHelpers.WriteList <Person>(femaleOnly));

            // select
            var onlyFirstName = dummyData.Select(person => person.FirstName);

            consoleText += descriptionValueFormat("Select linq method, only first names", DisplayFormatHelpers.WriteList <string>(onlyFirstName));


            // any
            var anyJohhnyFirstName = dummyData.Any(person => person.FirstName == "Hunter");

            consoleText += descriptionValueFormat("Any linq method, any person contains FirstName=\"Hunter\"", anyJohhnyFirstName.ToString());

            // take

            var takeExample = dummyData.Take(3);

            consoleText += descriptionValueFormat("Take the first 3 elements only", DisplayFormatHelpers.WriteList <Person>(takeExample));


            // skip
            var skipExample = dummyData.Skip(2);

            consoleText += descriptionValueFormat("Skip the first 2 elements", DisplayFormatHelpers.WriteList <Person>(skipExample));


            // first
            var firstExample = dummyData.FirstOrDefault(person => person.Gender == "F");

            consoleText += descriptionValueFormat("First female person in the list", firstExample.ToString());



            // single
            var singleExample = dummyData
                                .Where(person => person.Age == 19)
                                .SingleOrDefault();

            consoleText += descriptionValueFormat("Single Linq method (being explicit that you only expect 1 element)", singleExample.ToString());


            // combined example
            var combo = dummyData
                        .Where(p => p.Age >= 20)                      // filter items
                        .Skip(1)                                      // ignore the first item
                        .Take(2)                                      // only take 2 results from the collection
                        .Select(p => p.FirstName + " " + p.LastName); // return a specific value instead of the whole


            consoleText += descriptionValueFormat("Combo: .Where(p => p.Age >= 20)\n\t.Skip(1)\n\t.Take(2)\n\t.Select(p => p.FirstName + \" \" + p.LastName)", DisplayFormatHelpers.WriteList <string>(combo));


            return(consoleText);
        }