-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.

4
  • 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 Commented Sep 21, 2020 at 5:50
  • It´s literally in the duplicate: "The SqlDataReader type's constructor is marked as internal". Commented Sep 21, 2020 at 6: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. Commented Sep 21, 2020 at 6: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. Commented Sep 21, 2020 at 6:25

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Jack: Thanks I was so far looking at the Definition (F12) in the Visual Studio Code....thinking it would even show me the Constructor's metadata as well.
Ya, that just shows you the declarations.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.