Click here to Skip to main content
15,881,856 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
I have a question. Interface does not hold any definition. Interface can not be instantiated. How can this code be valid ?

IDataReader reader = cmd.ExecuteReader();


cmdExecuteReader returns an object with a value in memory. reader is interface. How can an object be assigned to an interface ? isn't interface just a contract with no method definitions inside ?
Posted
Updated 26-May-11 1:03am
v3

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())
    {
        // Do something with the reader
    }
}


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
 
Share this answer
 
Comments
Manfred Rudolf Bihy 26-May-11 8:12am    
Yes! My 5+
Kim Togo 26-May-11 8:16am    
My 5. Good answer.
Sandeep Mewara 26-May-11 10:34am    
My 5+!

BTW, guess what... OP reposted the same question. :doh:
Sergey Alexandrovich Kryukov 26-May-11 16:09pm    
Not 100% accurate. What's "retrieve"? This is about run-time vs compile-time types.
Please see my answer.
--SA
Dylan Morley 27-May-11 4:10am    
By retrieve I just meant 'assign to a variable of type Interface', probably wasn't worded best by me though.

I think it was right to attempt to show a simple 'why' example of Interfaces, I think OP is struggling to understand exactly where they fit in and the advantages they offer.
If you understand how pointers work you can easily understand this. Every object requries some memory when initiated and the compiler decides how much memory will be required on the schema of the class definition i.e. member variable & functions. Now interface too contains property & functions member singnature to be implemented.

Any reference type pointer requires same type of memory to be pointed that its schema defines. When an interface points to an object (i.e. memory location) it is actually pointing to the property and functions of that object because the signature matches. The interface variable maps to it actually.

InterfaceVariable.Function1'sLocation = Object.Function1'sLocation (if Function1 implements interface's Function1)<br />
InterfaceVariable.Function2'sLocation = Object.Function2'sLocation (if Function2 implements interface's Function2)


i answered a question that is not related to inteface but related to downcasting, it may help you understand it better what i'm talking about.

http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/a8fb7d67-6bbc-4cef-a175-44740ba3bef4/#780430cc-947e-403c-9167-dee53ddd3532[^]

Thanks,
Hemant
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-May-11 16:11pm    
This is more accurate answer, my 5.

You should have also mentioned: this is about run-time vs compile-time types.
Please see my answer.
--SA
Hemant__Sharma 27-May-11 2:34am    
Thanks @SAKryukov.

About run-time vs Compile-time, i doubt i could have explained it better than you :).
--Hemant
You never instantiate interface even in your example. Look at this as at regular late binding (http://en.wikipedia.org/wiki/Late_binding[^]).

Your IDataReader reader has the compile-time type of IDataReader. After initialization, the variable becomes an object of distictly different run-time type. It is only assigned-compatible with the interface type in the sense of OOP inheritance.

Your "interface does not hold any definition" is correct, "interface just a contract with no method definitions inside" is not accurate! You need to understand difference between interfaces as types and the variables (objects) of interface types, which can only be a compile types. Run types are always some objects implementing interfaces — classes of structures.

This is a major difference between variable of interface (or abstract class types) and non-abstract classes (or structures), where the run-type can be the same as compile type. For interface references, they are always different.

—SA
 
Share this answer
 
v4
Comments
Hemant__Sharma 27-May-11 2:31am    
Right...Right...Right.. My 5
Sergey Alexandrovich Kryukov 27-May-11 2:37am    
Thank you very much.
--SA
An interface cannot be instantiated directly[^].

IDataReader reader = new IDataReader();

I think this explains enough.
 
Share this answer
 
v2
Comments
Manfred Rudolf Bihy 26-May-11 8:11am    
I understand you perfectly.
I hope OP does so too!

My 5+
Manfred Rudolf Bihy 26-May-11 8:13am    
Proposed as the solution!
Sergey Alexandrovich Kryukov 26-May-11 16:10pm    
Not accurate! In fact, not even indirectly. This is about run-time vs compile-time types.
Please see my answer.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900