#314 – Access Modifiers Are Not Allowed on Static Constructors

Because you can’t call a static constructor directly, you can’t include an access modifier (e.g. public, private) when defining a static constructor.  Static constructors are defined without access modifiers.

        static Dog()
        {
            Motto = "We serve humans.  And lick ourselves.";
        }

The compiler will generate an error if you try to include an access modifier.

Advertisement

#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;
        }

#312 – Accessibility of Methods in a Class

You can apply access modifiers to methods defined in a class to define their accessibility.  Accessibility dictates what other code is allowed to call the method.

  • public – Any code can call the method
  • private – Only code in the defining class can call the method
  • protected – Code in the defining class or derived classes can call the method
  • internal – Any code in the defining assembly can call the method
  • protected internal – Code in the defining assembly or in derived classes can call the method
    public class Dog
    {
        public void BarkALot()
        {
            for (int i = 1; i < 30; i++)
                BarkOnce();
        }

        // Only this class can call
        private void BarkOnce()
        {
            Say("Woof");
        }

        // Subclass can call
        protected void BarkYourName()
        {
            Say(Name);
        }

        // Code in same assembly can call
        internal void DumpBarkCount()
        {
            Say(numBarks);
        }

        // Code in same assembly or subclass can call
        protected internal void BarkNameTwice()
        {
            Say(Name);
            Say(Name);
        }
    }

#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; }
    }

#310 – Accessibility of Fields in a Class

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

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

        // Only code in this class can access
        private string genericDogSecretName;

        // Code in this class or subclass can access
        protected int totalBarkCount;

        // Code in same assembly can access
        internal int invokeCount;

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

#309 – Accessing Protected Members In a Derived Class

A member of a class with its accessibility set to protected is accessible from within the class that defines the member and from within classes that derive from the defining class.

You’d typically access protected instance members through the this reference in the defined class.

    public class Dog
    {
        protected string locationOfBone;
    }

    public class Terrier : Dog
    {
        public void DivulgeTerrierSecrets()
        {
            // CAN access protected member through implicit this reference
            Console.WriteLine("Bone is: {0}", locationOfBone);
        }
    }

However, when the subclass is accessing the protected member, it must do so through an instance of the subclass and not through an instance of the parent class.

    public class Terrier : Dog
    {
        public static FindSecret()
        {
            Terrier t = new Terrier("Jack");
            string s = t.locationOfBone;   // OK

            Dog d = new Dog("Spot");
            string s = d.locationOfBone;   // Not OK - ERROR
        }
    }

#308 – Protected Internal Class Members

Class members marked with the accessibility keyword protected internal are accessible from within the same class, from within code in classes that derive from the class, or from within code in classes within the same assembly.

In the picture below, the Dog.DoBark method is marked as protected internal.  The code in any of the blue blocks can call this method.

#307 – Internal Class Members

Class members marked with the accessibility keyword internal are accessible from within the same class, or from within the code of any classes that exist in the same assembly.

In the picture below, the Dog.DoBark method is marked as internal.  The code in any of the blue blocks can call this method.

Notice that code in the Shepherd class cannot call DoBark, even though it’s in a subclass of Dog.  Also notice that code in OtherClass can call DoBark, even though it’s not a subclass of Dog.  Only code in the same assembly as the Dog class has access to the DoBark method.

#306 – Protected Class Members

Class members marked with the accessibility keyword protected are accessible from within the same class, or from within the code of any classes that derive from the class.

In the picture below, the Dog.DoBark method is marked as protected.  The code in any of the blue blocks can call this method.

Code in a subclass of Dog can call the DoBark method.

    public class Terrier : Dog
    {
        public void TerrierBark()
        {
            // CAN call DoBark from subclass
            this.DoBark();
        }
    }

Code in other classes, not derived from Dog, cannot call the DoBark method.

    public class OtherClass
    {
        public void DoSomething()
        {
            Dog d = new Dog();
            // This class CANNOT call DoBark()
            d.DoBark();
        }
    }

#305 – Public Class Members

Class members marked with the accessibility keyword public are accessible from any code.

In the picture below, the Dog.DoBark method is marked as public.  The code in any of the blue blocks can call this method.