ExecuteReader
doesn't return an object - it returns a reference to an object of some type which implements IDataReader
(or null, of course).
The idea is that the caller/client doesn't need to know about the implementation class, just that it implements the interface. When the client calls a method such as reader.Next()
, that will use the implementation based on the execution-time type of the object that the value of reader
refers to.
Assigning a reference value to a variable doesn't change the type of object to which that reference refers. For example:
string text = "hello";
object o = text;
Now o
and text
have the same value - a reference to the same string. If you call:
Type t = o.GetType();
that will still return a reference to the Type
object representing System.String
, because the value of o
refers to a String
object; the type of the variable through which you access the object doesn't change the execution-time type of the object.