#313 – Accessibility of Constructors in a Class

You can apply access modifiers to instance constructors, dictating what code is allowed to create an instance of the class using a signature that matches the constructor.

  • public – All code has access
  • private – Only code in defining class has access
  • protected – Code in defining class or derived classes has access
  • internal – All code in defining assembly has access
  • protected internal – Code in defining assembly or in derived classes has access
        // All code
        public Dog(string name, int age)
            Name = name;
            Age = age;

        // Only code in this class has access
        private Dog()
            Name = "UNKNOWN";
            Age = 0;

        // Code in this class or subclass has access
        protected Dog(string name)
            Name = name;
            Age = 1;

        // Code in same assembly has access
        internal Dog(int age)
            Name = "INTERNAL";
            Age = age;

        // Code in same assembly or derived classes has access
        protected internal Dog(double age)
            Age = (int)age;

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: