Click here to Skip to main content
16,012,107 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In The .NET Framework

SqlDataReader class which property have that why i can't Inherit it

or can we make class with out seal Keyword That is not inherited in our application like SqlDataReader Class That is also Public class



C#
// Summary:
    //     Provides a way of reading a forward-only stream of rows from a SQL Server
    //     database. This class cannot be inherited.
    public class SqlDataReader : DbDataReader, IDataReader, IDisposable, IDataRecord
     
     {
      //
      //
      //
      //
      .
      .   
      .
      .
      .

     }
Posted
Updated 13-Oct-11 23:26pm
v2

 
Share this answer
 
You can create a class that can't be inherited if it has no constructors that a subclass can see (i.e. nothing protected or public). You can create one that can't be inherited outside the assembly it resides in by having only internal (or protected internal) constructors. I would say, though, that if your intention is to make a class non-inheritable, you should mark it as sealed (and Microsoft should have done so here unless it is internally inheritable, which it probably is).
 
Share this answer
 
Comments
anil_bang0011 14-Oct-11 11:35am    
ok if we use out side of assembly i think all is is not inherited even they are public

but in case SqlDataReader we use with in assembly
and we can't access in sub class
in C# by default create constructors

public class MyClass
{

}
class Program :MyClass
{
static void Main(string[] args)
{

}
}

how can i avoid MyClass is not inherited in Program class
BobJanova 14-Oct-11 12:54pm    
This is an English language site >_>

To make MyClass non-inheritable, you should mark it sealed.
The SqlDataReader class is not sealed, but you cannot inherit from it as it is not a class you can instantiate. I.e. you cannot say:
C#
SqlDataReader sdr = new SqlDataReader();

If you think about it, it makes a lot of sense. A data reader is only used to read all the information returned from a SQL Command execution, so creating a new instance of it completely useless - it has no SQL results to read through. So, the only way you can get an instance is by requesting it from an SQLCommand by using the ExecuteReader method.

So, if you can't create an instance, why would it be possible to inherit from it? You can't create the inherited instance at all - you cannot "upgrade" a class to a derived version, as the framework would have to "invent" all the missing information.

What are you trying to achieve, that you think this would be a good idea?
 
Share this answer
 
Comments
anil_bang0011 14-Oct-11 7:59am    
I did not going to achieve anything .. i just read the framework that why i have a Question

and SqlDataReader sdr = new SqlDataReader();
is also provide by the Sealed Class that is we always done with String

i just ask just which property make its Un - Inherited
and nothing more i want to .....

Thanks
OriginalGriff 14-Oct-11 8:45am    
Good for you! There is technically no property that makes it un-inheritable; it is the specific lack of an accessible constructor that prevents it - unlike a string, which is a sealed class and nothing can inherit from it. The chances are that in practice the SQLDataReader constructors are declared as internal, so that it can be inherited from within same namespace. Why? It could be as simple as Microsoft forgot to mark it as sealed, or it could be that there are "hidden" classes that internally inherit from it.
If you want to do the same, I would strongly suggest using sealed instead if you can, as it make it more obvious to anyone trying to use your class.
anil_bang0011 14-Oct-11 11:23am    
Hi Thanks alot's your answer

Thanks :) :)
anil_bang0011 14-Oct-11 11:37am    
if you saying Microsoft forgot to mark is sealed .. I think its a good Joke


public class MyClass
{

}
class Program :MyClass
{
static void Main(string[] args)
{

}
}


how can i avoid MyClass is not inherited in Program class
OriginalGriff 14-Oct-11 12:08pm    
Sorry? That doesn't make a lot of sense. You can inherit from MyClass in Program - it will compile cleanly, and work perfectly well.
Strange thing to do, but it will work.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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