I know that IDataReader is a interface so I can’t create an instance of interface.
However, it is possible to get instances of IdataReader.
How can this be possible?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You cannot create an instance of an Interface. But you can create an instance of a class that implements an interface. In case of
IDataReaderSqlDataReaderis an implementation.You get an instance by executing a SqlCommand:
For example (from MSDN):
Edit:
I assume that you don’t understand why it’s possible to have an instance that has the type of an interface. That is no contradiction since an interface actually is a type.
So if i would create a class, say
MyDataReader, that implementsIDataReader, an instance of my class would be of typeMyDataReaderas well asIDataReader.If anybody would create an instance of
MyDataReader, he knows that it also implements all methods ofIDataReader. That is important for Polymorphism.