Pranay Rana: collection
Showing posts with label collection. Show all posts
Showing posts with label collection. Show all posts

Monday, September 7, 2015

Collection Interface In .NET

Introduction

.NET framework provides interfaces that implements by collections in language to provide functionality of iterating over objects in collection, adding and removing object from collection to randomly access object from collection.

As different interfaces provide different set of functionality most of the developers has problem when to use which interface to achieve functionality. The following post provides information about interfaces implemented by collection.

Interfaces

The following diagram is for relation between the interfaces.


Note:
  1. Class diagram are not having all the methods but contains important method that belongs to each collection interface.

  2. Collection interface is available in both generic and non-generic form, so in diagram obj type is object in nongeneric form and obj type is T(template type) in generic form.
Functionality Provided Read Count Add & Remove Index Based Read Index Based Add & Remove
IEnumerable
  1. Provide Read only Collection.
  2. Allow to read each object of collection in forward only mode.
Y N N N N
ICollection
  1. Allow to modify collection.
  2. Allow to get size of collection.
  3. Allow to Add and Remove object in/from collection.
Y
Inherited)
Y Y N N
IReadOnlyCollection
  1. Allow to read collection.
  2. Allow to get size of collection.
Y
(Inherited)
Y N N N
IList
  1. Allows to access collection by Index.
  2. Allow to Add and Remove object in/from collection by index.
Y
(Inherited)
Y
(Inherited)
Y
(Inherited)
Y Y
IReadOnlyList Allow to read collection by Index. Y
(Inherited)
Y
(Inherited)
N Y N

Note:
In above diagram (Inherited), the columns indicate that the features are inherited from parent and to find out from which parent one must look in the interface collection diagram.

So from above table three main interfaces functionality concluded in following way:

IEnumerable – interface provide minimum functionality which is Enumration.

ICollection – interface provide medium functionality which is getting size, adding, removing and clearing collection i.e. modification of collection. As it inherited from IEnumerable so includes functionality of IEnumerable.

IList – interface provide full functionality which is index base accessing of collection element, index base adding, index base removing from collection. As it inherited from ICollection it includes functionality of Enumerable and ICollection.

The following are some important things to know:
  1. IEnumerable interface under the hood make use of IEnumerator for providing reaonly and forward mode read.

  2. IReadOnly*** and IEnumerable are used for providing readonly collection. But difference is that IEnumerable allows collection to read in forward only mode where IReadOnly*** provide feature of Collection /List but only in readonly mode i.e. without modification feature like add & remove.

  3. IReadOnly is part of collection interface from framework 4.5.
Above table list down the features provided by each interface when collection gets converted to interface type or class implement interface to provide feature of collection.

Conclusion

It’s very important for developers to understand these interfaces because the rule says its always good to depend on interface rather than on the concrete type.

Tuesday, July 12, 2011

ForEach Method for the collections

In this small post I am going to discuss about the new method introduce in the .net framework ForEach which allow to perform the action on the each set of element.

Syntax
public void ForEach(Action<t> action)
Action<t> is delegate or function to perform on each element of the List.

To get in more detail how it works check check the following code.
Below is Employee class which has property related to employee of the company.
public class Employee
{
        public string Name { get; set; }
        public int Salary { get; set; }
        public string Address { get; set; }
        public int Id { get; set; }
}
and now in following line of code I am initializing list of employees
class Program
    {
        static void Main(string[] args)
        {
          List<employee> emplst = new List<employee> { 
          new Employee() { Name="pranay", Address = "Ahmedabad", Id=1, Salary=1234},
          new Employee() { Name="Hemang", Address = "Ahmedabad", Id=2, Salary=2348},
          new Employee() { Name="kurnal", Address = "Himmatnagar", Id=3, Salary=8999},
          new Employee() { Name="Hanika", Address = "Ahmedabad", Id=4, Salary=7888}
        };
Now I want to increase salary of each employee by 10%, so with the the help of new ForEach construct I can easily achieve it by using following line of code.
emplst.ForEach(new Program().incSalary);
     }

  private void incSalary(Employee emp)
  {
     emp.Salary += (emp.Salary * 10)/100; 
  }
}
As you can see in above code I have written new Program().incSalary as action, in the incsalary method as you see I increase the salary of each employee 10%.
This thing can easily also done by making use of the foreach loop available but if you see the the ForEach in reflector it does the same thing.
ForEach method make code more simple and easy to understand.