#311 – Accessibility of Properties in a Class

You can apply access modifiers to properties defined in a class to define their accessibility.  Accessibility dictates what other code is allowed to read and the write the value of a property.

  • public – All code can read/write the property
  • private – Only code in the defining class can read/write the property
  • protected – Code in the defining class or derived classes can read/write the property
  • internal – All code in the defining assembly can read/write the property
  • protected internal – Code in the defining assembly or in derived classes can read/write the property
    public class Dog
    {
        // All code can access
        public string Nickname { get; set; }

        // Only code in this calss can access
        private string genericDogSecretName { get; set; }

        // Code in this class or subclass can access
        protected int totalBarkCount { get; set; }

        // Code in same assembly can access
        internal int invokeCount { get; set; }

        // Code in same assembly or derived classes can access
        protected internal int barkInvokeCount { get; set; }
    }
Advertisements

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

One Response to #311 – Accessibility of Properties in a Class

  1. Pingback: #796 – Default Accessibility for Property Accessors | 2,000 Things You Should Know About C#

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 )

Google+ photo

You are commenting using your Google+ 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: