#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