none
What is the significance of protected constructor in c sharp and in which context should I declare a constructor as protected? RRS feed

  • Question

  • Hi there

    protected Assembly();

    protected Type();

    I just want to know why Assembly class and Type class have protected constructors?..

    When should I declare a constructor as protected, in general?

    I have one more question --What is the significance of protected keyword inside System.Object class? Because every class will be inheriting it right?

    protected object MemberwiseClone();

    Sunday, October 28, 2012 12:12 PM

Answers

  • protected (C# Reference)

    The keyword "protects" the class from having its' constructor called by external classes.  However unlike the private keyword, protected will allow derived classes to access the class member.

    So what good is it?  Classes that use it will employ other means to create instances of the class.  A static "CreateInstance" method within the class can call the protected constructor and return an instance. 

    Accessibility Levels (C# Reference) 

    Hope this helps.

    Rudy   =8^D

      

    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.com/

    Sunday, October 28, 2012 11:07 PM

All replies

  • protected (C# Reference)

    The keyword "protects" the class from having its' constructor called by external classes.  However unlike the private keyword, protected will allow derived classes to access the class member.

    So what good is it?  Classes that use it will employ other means to create instances of the class.  A static "CreateInstance" method within the class can call the protected constructor and return an instance. 

    Accessibility Levels (C# Reference) 

    Hope this helps.

    Rudy   =8^D

      

    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.com/

    Sunday, October 28, 2012 11:07 PM
  • I think a more frequent use of protected constructors is to make it clear that you are not supposed to create instances of this class. The class is merely intended to serves as a base class for derived classes (which can access the constructor, since it's protected).
    Tuesday, December 13, 2016 9:24 AM