Showing posts with label Interface. Show all posts
Showing posts with label Interface. Show all posts

Monday, June 3, 2013

IEnumerable Vs IQueryable

IQueryable

The
IQueryable interface allows the objects to be queried based on the LINQ-to-SQL. Objects which implement the IQuerieable interface can be further queried using LINQ queries and these additional queries will be executed on the underlying data source.

The below line first queries the list of employees from the datasource, the resultant object empList is further queried based on the department name of the employee, both the filters are done in the underlying datasource and only the final list of employees belonging to the sales department are returned and stored in the memory, using LINQ-to-Sql. Hence more efficient based on
IEnumerable based objects.

IQueriable Interface

The IQueriable interface is used to execute queries against a data source, where the type of ata returned from the source is not known at design time.

IQueriable interface inherits from the IEnemurable interface, and allows the objects to be queried based on the underlying data source, hence they are more efficient when the types are to be queried/filtered further.

Namespace:  System.Linq 

IEnumerable T Interface

The IEnumerable interface exposes the enumerator, which supports a simple iteration over a collection of a specified type.

Namespace:  
System.Collections.Generic.

While using IEnumerable we provide the type along with the data, represents the type of data which will be stored in the collection.

Sunday, June 2, 2013

IEnumerable Interface

The IEnumerable interface exposes the enumerator, which supports a simple iteration over a non-generic collection.

Namespace:  System.Collections

.Net Framework pre-defined Interfaces

The .Net framework itself is built with a number of interfaces, each of these interfaces are pre-defined and have a specific purpose. The classes which implement these interfaces should override the methods in these interfaces, this way .Net makes sure that all the classes which implement the interfaces are consistent. The classes can add their own methods but they should definitely implement the methods defined in the interface.

The following are some of the commonly used interfaces which are per-defined in the .Net framework.

Accessing Methods of Explicit Interfaces

We have seen on how to implement more than one interface which have methods/properties with the same name and signature using explicit interfaces. Now how do we create instance of the class and call these methods individually.

The following example contains 2 interfaces IEmployee and IDepartment, both the interfaces contain a method GetName() with the same name and signature. The class OfficeClass implements both the interfaces and implements the GetName method from both the interfaces using Explicit Interface Implementation.

Explicit Interface Implementation

We know that a class can implement more than one interface, now what if a class implements 2 interfaces and if both the interfaces contain a method with the same name and signature, how do we implement these methods in the class body. This is where explicit Interface comes into play. In such cases the method name should be prefixed with the interface name and a (.) symbol.

The following example contains 2 interfaces IEmployee and IDepartment, both the interfaces contain a method GetName() with the same name and signature. The class OfficeClass implements both the interfaces and implements the GetName method from both the interfaces using Explicit Interface Implementation.

Interface Inheritance

Similar to class inheritance, interfaces also support inheritance, an interface can inherit from another interface. Similar to class inheritance, interface inheritance also makes use of the (:) operator to define inheritance.

The following example defines a parent interface IVehicle, and a child interface IBus, the IBus interface inherits from the IVehicle interface.

What are Interfaces?

An Interface defines a set of methods and properties which should be implemented in the class which implements the interface. Interfaces help us in implementing the concept of multiple inheritances in C#. In C# a class cannot derive from more than one parent class, but can implement more than one interface, hence they help us in implementing multiple inheritance.

In the following example the Interface IEmployee defines 2 methods and the class EmployeeClass implements this interface and adds the method definition to both these methods declared in the interface.