Saturday, October 5, 2013

I Emumabrable Example

I Emumabrable:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace Demos
{
    //In Heric the List Collection from IEnumerable<T> Collection
    //Implemt the Interfaceto  the IEnumerator<T> GetEnumerator()
    class clsMylist<T> : IEnumerable<T>
    {
        T[] arr;
        int size;
        int count;

        //1
        //Creating Public proeprty
        public int Count
        {
            get { return count; }
            set { count = value; }
        }

        //2
        //Constructer of the same class
        //Is like a method but no return type
        public clsMylist()
        {
            //Access the property
            //assign value to property
            size = 5;
            arr = new T[size];
        }

        //3
        //Method in Class
        public void Add(T data)
        {
            arr[count] = data;
            count++;
        }
       
        //step 5(a)
        //Defining the Indexer for T(Because we dont knwoe the dataType of T)
        public T this[int x]
        {
            get
            {
                //beacuse we are storing the list in array so that
                //X is the index for list
                return arr[x];
            }
            set
            {
                arr[x] = value;
            }
        }


        //Step 6
        public IEnumerator<T> GetEnumerator()
        {
            for (int i = 0; i < arr.Length; i++)
            {
                yield return arr[i];
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }
    }

    class TestMylist
    {
        static void Main()
        {

            //Step 4
            clsMylist<int> nums = new clsMylist<int>();
            nums.Add(10);
            nums.Add(20);
            nums.Add(30);
            nums.Add(40);
            nums.Add(50);

            /* Console.WriteLine(nums[2]);
           for (int i = 0; i < nums.Count-1; i++)
           {
               Console.WriteLine(nums[i]);
           }
           //Indexer Concept
            
            */
            //Step 5(c)
            clsMylist<Employee> emps = new clsMylist<Employee>();
            emps.Add(new Employee { EmpNo = 1010, EmpName = "Venu", Dept = "HR", Salary = 12000 });
            emps.Add(new Employee { EmpNo = 1010, EmpName = "Venu", Dept = "SWE", Salary = 12000 });
            emps.Add(new Employee { EmpNo = 1011, EmpName = "srinu", Dept = "CISF", Salary = 13000 });
            emps.Add(new Employee { EmpNo = 1012, EmpName = "Shekar", Dept = "BSF", Salary = 14000 });
            emps.Add(new Employee { EmpNo = 1013, EmpName = "Umakar", Dept = "Student", Salary = 15000 });

            /*  for (int i = 0; i < emps.Count; i++)
              {
               //Not require IEnumerator<> collection
              } */


            //Step 7(b)
            //foreach statement cannot operate on variables of type 'Demos.clsMylist<Demos.Employee>' because 'Demos.clsMylist<Demos.Employee>' does not contain a public definition for 'GetEnumerator'
            //For the above E
            foreach (Employee emp in emps)
            {
                Console.WriteLine("for IEmumarable   " + emp);
            }
            Console.ReadLine();

        }

    }


    class Employee
    {

        //Step 5(b)
        public int EmpNo { get; set; }
        public string EmpName { get; set; }
        public string Dept { get; set; }
        public int Salary { get; set; }


        //Step 7(a)
        public override string ToString()
        {
            return string.Format("{0},{1},{2},{3}", EmpNo, EmpName, Dept, Salary);
        }
    }



}

No comments: