Friday 14 March 2014

IEnumerable and IEnumerator

IEnumerable:


We have create UI, Business Layer and Data Layer. Data Layer return a list, That list do some manipulation (changes)  in BL. BL to UI this list return IEnumerable<>. Because I (BL Developer)  thought UI Developer unable to change the value. Security propose we can use IEnumerable, Because IEnumerable object unable to Add,Remove,ect...

Difference between IEnumerable and List properties 


Example List 


IEnumerable VS IEnumerator
      Ienumerator also unable to Add and remove ect... Values. But here we can't use lambda expression. 

enumerator is created or after the Reset method is called, an enumerator is positioned before the first element of the collection, and the first call to the MoveNextmethod moves the enumerator over the first element of the collection.
If MoveNext passes the end of the collection, the enumerator is positioned after the last element in the collection and MoveNext returns false. When the enumerator is at this position, subsequent calls to MoveNext also return false until Reset is called.
An enumerator remains valid as long as the collection remains unchanged. If changes are made to the collection, such as adding, modifying, or deleting elements, the enumerator is irrecoverably invalidated and the next call to MoveNext or Reset throws an InvalidOperationException.


Code :

using Generic.Code;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace Collection.Sample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Btn_IList_Click(object sender, EventArgs e)
        {
            CollectionAll obj = new CollectionAll();
            IEnumerable<ProEmp> objreturn = obj.RetIEnumerableValue();
            var tt= objreturn.AsParallel();
            objreturn.Where(n=>n.Id == 103);
            var item1 = (objreturn.Where(n => n.Id == 102)).FirstOrDefault();
            MessageBox.Show("Id = " + item1.Id + "   Name  = " + item1.Name);
        }

        private void btnList_Click(object sender, EventArgs e)
        {
            CollectionAll obj = new CollectionAll();
            List<ProEmp> objreturn = obj.RetListValue();
           
            objreturn.Where(n => n.Id == 103);
            var tt = objreturn.Where(n => n.Id == 102);
        }

        private void btn_IEnumerator_Click(object sender, EventArgs e)
        {
            CollectionAll obj = new CollectionAll();
            IEnumerator<ProEmp> objreturn = obj.RetIEnumeratorValue();
           
            while (objreturn.MoveNext())
            {
                ProEmp item = objreturn.Current;
                MessageBox.Show("Id = " + item.Id + "   Name  = " + item.Name);
                if (item.Id == 103)
                    break;
            }
            MessageBox.Show("While loop End");
            objreturn.MoveNext();
           
            ProEmp item1= objreturn.Current;
            MessageBox.Show("Id = " + item1.Id + "   Name  = " + item1.Name);

          

        }

        private void btnDelegates_Click(object sender, EventArgs e)
        {
            CollectionAll obj = new CollectionAll();
            MessageBox.Show(obj.Calc(15, 8).ToString());
        }
    }
}


Class Code :

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

namespace Generic.Code
{
    public class CollectionAll
    {

        List<ProEmp> objemp;
        public IEnumerable<ProEmp> RetIEnumerableValue()
        {
            Setvalue();
            return objemp;
        }

        public List<ProEmp> RetListValue()
        {
            Setvalue();
            return objemp;
        }

        public IEnumerator<ProEmp> RetIEnumeratorValue()
        {
            Setvalue();
            return objemp.GetEnumerator();
        }

        private void Setvalue()
        {
            objemp = new List<ProEmp>();
            ProEmp obj = new ProEmp();
            obj.Id = 101;
            obj.Name = "Sriram";
            obj.Salary = 20000;
            obj.Address = "Chennai";
            objemp.Add(obj);
            obj = new ProEmp();
            obj.Id = 102;
            obj.Name = "Bala";
            obj.Salary = 200000;
            obj.Address = "Chennai";
            objemp.Add(obj);
            obj = new ProEmp();
            obj.Id = 103;
            obj.Name = "Azeez";
            obj.Salary = 150000;
            obj.Address = "Madurai";
            objemp.Add(obj);
            obj = new ProEmp();
            obj.Id = 104;
            obj.Name = "Ratheesh";
            obj.Salary = 200000;
            obj.Address = "Madurai";
            objemp.Add(obj);
            obj = new ProEmp();
            obj.Id = 105;
            obj.Name = "Vikash";
            obj.Salary = 180000;
            obj.Address = "Chennai";
            objemp.Add(obj);
        }

    }
}


namespace Generic.Code
{
    public class ProEmp
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public double Salary { get; set; }
        public string Address { get; set; }
    }
}