-2

There is an need to appreciate how C# has made SqlDataReader class Non Inheritable and Non Instantiable.

Namespace: System.Data.SqlClient

SqlDataReader class header :

public class SqlDataReader : System.Data.Common.DbDataReader, IDisposable, System.Data.Common.IDbColumnSchemaGenerator{

}

Question 1: The class seen is public.

Since the class is public :: So why can't it be inherited.

Question 2: Constructor is not Private : Infact on seeing the definition of SqlDataReader : no constructor was seen :: So i guess the Default constructor that C# will label to it to be default would be a Public one which happens when no constructor is found :: So despite, supposedly, having a Public constructor why should the Instantiation not be possible.

Besides I can't find the constructor to be "internal" as was answered and accepted in this question link : why cant SqlDataReader class be inherited?

Would be highly appreciative if the point of error in the above thinking can be highlighted.

  • 1
    "Constructor is not Private" how do you know? Do you have access to the source code?. Also, your linked question does not state that the class is internal, only the *constructor*. Which means that the class is visible from outside the `System.Data` dll, but cannot be instantiated from outside it, meaning you also cannot inherit it as any class inheriting from it cannot instantiate its base class – MindSwipe Sep 21 '20 at 05:50
  • It´s literally in the duplicate: "The SqlDataReader type's constructor is marked as internal". – MakePeaceGreatAgain Sep 21 '20 at 06:00
  • "internal SqlDataReader(SqlCommand command, CommandBehavior behavior)": From the source-code: `internal SqlDataReader(SqlCommand command, CommandBehavior behavior) { ... }`. How did you examine the "sourcecode"? Probably with some tool that just decompiles an assembly. However that does not neccessarily give youe the **actual** code. – MakePeaceGreatAgain Sep 21 '20 at 06:15
  • HimBromBeere : Thanks for answering : Yes it was a duplicate and needed an answer for someone who has started to begin questioning on basics quite late. However the question did not allow me to ask again as i had no points. In just few mins even this question was closed by some other member i guess. Any suggestions wht better course i could have followed. – user1898135 Sep 21 '20 at 06:25

1 Answers1

0

I am not sure which source code you are looking at (Probably a basic disassembly). Have a look at the github SqlDataReader Source

It has internal constructor only and hence it cant be instantiated.

Code Name Jack
  • 2,856
  • 24
  • 40