Friday, 2 March 2012

Can we Create a Custom Collection List?

Yes, we can also create our own custom collection like List<T>.
Ex:

class Employees<a>
    {
        a id, salary;
        public Employees(a x, a y)  // parameterized constructor          
        {
            this.id = x;
            this.salary = y;
        }
        public void print()
        {
            Console.WriteLine("id:{0}\tsalary:{1}", id, salary);
        }
    }

but i want enumerate through the that collection menas we have to implement IEnumerable<T> interface also.

  class Emplyee
    {
        static void Main()
        {
            Employees<int> obj1 = new Employees<int>(123,5000);
            Employees<int> obj2 = new Employees<int>(147, 4500);          
            obj1.print();
            obj2.print();
            Console.ReadKey();
        }
    }

No comments:

Post a Comment