Exemple #1
0
        private void RunStackGenerics()
        {
            // A Stack generic type represents a Last-in, first-out (LIFO) collection of objects.

            Stack <GeneralExampleClass> myStack = new Stack <GeneralExampleClass>();

            // here we are adding objects to the Queue list the only way you can via the Enqueue method.
            GeneralExampleClass blah1 = new GeneralExampleClass()
            {
                MyPublicProperty = "blah 1"
            };

            myStack.Push(blah1);
            Console.WriteLine("Added Stack Item: blah 1");

            myStack.Push(new GeneralExampleClass()
            {
                MyPublicProperty = "blah 3"
            });
            Console.WriteLine("Added Stack Item: blah 3");

            myStack.Push(new GeneralExampleClass()
            {
                MyPublicProperty = "blah 5"
            });
            Console.WriteLine("Added Stack Item: blah 5");

            myStack.Push(new GeneralExampleClass()
            {
                MyPublicProperty = "blah 2"
            });
            Console.WriteLine("Added Stack Item: blah 2");



            Console.WriteLine("Stack Items:");
            Console.WriteLine("-----------------");
            Console.WriteLine("");

            while (myStack.Count > 0)
            {
                GeneralExampleClass generalExampleClass = myStack.Pop();
                Console.WriteLine(String.Format("Popped Stack Item: {0}", generalExampleClass.MyPublicProperty));
            }

            if (myStack.Count == 0)
            {
                Console.WriteLine("Stack is empty!");
            }

            Console.WriteLine("");
            Console.WriteLine("");
        }
Exemple #2
0
        private void RunQueueGenerics()
        {
            // A Queue generic type represents a first-in, first-out (FIFO) collection of objects.

            Queue <GeneralExampleClass> myQueue = new Queue <GeneralExampleClass>();

            // here we are adding objects to the Queue list the only way you can via the Enqueue method.
            GeneralExampleClass blah1 = new GeneralExampleClass()
            {
                MyPublicProperty = "blah 1"
            };

            myQueue.Enqueue(blah1);
            Console.WriteLine("Added Queue Item: blah 1");

            myQueue.Enqueue(new GeneralExampleClass()
            {
                MyPublicProperty = "blah 3"
            });
            Console.WriteLine("Added Queue Item: blah 3");

            myQueue.Enqueue(new GeneralExampleClass()
            {
                MyPublicProperty = "blah 5"
            });
            Console.WriteLine("Added Queue Item: blah 5");

            myQueue.Enqueue(new GeneralExampleClass()
            {
                MyPublicProperty = "blah 2"
            });
            Console.WriteLine("Added Queue Item: blah 2");


            // we can peak a queue item to look at the next item inside
            Console.WriteLine("Peak Queue Items:");
            Console.WriteLine(String.Format("Peak Item: {0}", myQueue.Peek().MyPublicProperty));
            Console.WriteLine("");

            // we can also do a contains on queues
            Console.WriteLine("Queue Contains:");
            Console.WriteLine(
                String.Format("Does Queue Contain blah 1? {0}",
                              myQueue.Contains(blah1) ? "True" : "False"
                              ));
            Console.WriteLine("");



            Console.WriteLine("Queue Items:");
            Console.WriteLine("-----------------");
            Console.WriteLine("");

            while (myQueue.Count > 0)
            {
                GeneralExampleClass generalExampleClass = myQueue.Dequeue();
                Console.WriteLine(String.Format("Dequeued Item: {0}", generalExampleClass.MyPublicProperty));
            }

            if (myQueue.Count == 0)
            {
                Console.WriteLine("Queue is empty!");
            }

            Console.WriteLine("");
            Console.WriteLine("");
        }