stackoverflow1453

Programming&&Games&&Life

Generic Collections

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GenericCollectionSample
{
    class Employee
    {
        public string Name;
        public int Age;
        public string Address;
    }

    class Program
    {
        static void Main(string[] args)
        {
            //Index based generic collection
            List<int> listObj = new List<int>();
            listObj.Add(23);
            listObj.Add(444);
            //Displaying list value using index
            Console.WriteLine("List Second Value: {0}", listObj[1]);


            //Key based generic collection
            Dictionary<int, string> objDic = new Dictionary<int, string>();
            objDic.Add(23, "Alo");
            Console.WriteLine("Printout the 23th element in dictionary: {0} ", objDic[23]);

            //Priority based generic collection(Stack)
            Stack<int> s1 = new Stack<int>();
            s1.Push(4);
            s1.Push(5);
            s1.Push(2);

            foreach (var item in s1)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine("1st value which is gotten from stack: {0}", s1.Pop());
            Console.WriteLine("2nd value which is gotten from stack: {0}", s1.Pop());

            //Priority based generic collection (Queue)
            Queue<int> q1 = new Queue<int>();
            q1.Enqueue(32);
            q1.Enqueue(45);
            q1.Enqueue(67);

            foreach (var item in q1)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine("1st guy which leaves the queue 1st: {0}", q1.Dequeue());

            //Creating Employee Records
            Employee e1 = new Employee();
            e1.Name = "Baran";
            e1.Age = 23;
            e1.Address = "Turkiye";

            Employee e2 = new Employee();
            e2.Name = "Taran";
            e2.Age = 265;
            e2.Address = "Yugoslavya";

            //Create generic list with employee records
            List<Employee> le1 = new List<Employee>();
            le1.Add(e1);
            le1.Add(e2);

            foreach (var item in le1)
            {
                Console.WriteLine(item.Name + " " + item.Age + " " + item.Address);
            }

            Console.ReadKey();
        }
    }
}

gen

Advertisements

No comments yet»

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: