You can retrieve an interface from any object that implements it.
E.g all the following command objects which are concrete classes for a particular type of database access will implement
IDataReader
IDataReader reader = iDB2Command.ExecuteReader();
IDataReader reader = OleDbCommand.ExecuteReader();
IDataReader reader = SqlCommand.ExecuteReader();
This means you could define a method that takes an IDataReader as an argument to process the reader
public void ProcessReader(IDataReader reader)
{
while (reader.Read())
{
}
}
Like you mention, the
IDataReader
interface holds a set of contracts that any classes that implement it must define.
Therefore, regardless of the underlying concrete class passed to the ProcessReader method, we know that certain methods have to be available, such as the
Read method
This allows you to write generic code that can handle lots of different concrete types