You must Sign In to post a response.
  • Category: .NET

    What are the access specifier can be used in class

    Hai All,

    May i know what are the access specifier can be used to create a class

    Default access specifier of class is internal

    we can change as public, but my doubt is ... when i change class access specifier to protected,private,protected internal it gives error

    Thanks and Regards,
  • #769045

    Changing the access modifier of a method in a derived type is pointless that's why it's not allowing to do so.

    class Base
    public virtual void A() {}

    class Derived: Base
    protected override void A() {}

    Now here if you use protected,

    public class A
    protected int x;

    public class B
    public static A CreateClassA()
    A x = new A();
    x.x = 5; // ERROR : No privilege
    return x;


  • #769048

    Hai Chellappan,
    The default access specifier for the class is internal and we can make it public also. Only these two access specifiers will work for the main class. You can't make the main class or the outer class as private, protected or protected-internal etc.
    But you can use other access specifiers with the internal or nested classes. Like below:

    internal class A
    protected class B
    // some code

    Again another point is that if the outer class scope is internal(means it can be access in the same assembly/project), the inner class cant be the public(bigger scope than outer class). So if the outer class is public, you can use any access specifier for the internal class(i.e. public, internal, protected etc).
    Hope it will be helpful to you.

    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)

  • #769049
    Following access modifiers can be used in class
    The type or member can be accessed by any other code in the same assembly or another assembly that references it.

    The type or member can be accessed only by code in the same class or struct.

    The type or member can be accessed only by code in the same class or struct, or in a class that is derived from that class.

    The type or member can be accessed by any code in the same assembly, but not from another assembly.

    protected internal
    The type or member can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.

    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #769080
    Access specifiers are keywords in object-oriented languages that set the accessibility of classes, methods, and other members. Example of Access specifiers is given in this code snippet
     sing namespace std;

    class Line {
    double length;
    void setLength( double len );
    double getLength( void );
    double Line::getLength(void) {
    return length ;

    void Line::setLength( double len ) {
    length = len;

  • Sign In to post your comments