Search This Blog

Friday, December 14, 2012

protected C#

C# > Access Modifiers > Protected

The protected access modifier is between the private and public and is accessible from within the class in which it is declared, and from within any class derived from the class that declared this member.

Example:

namespace WFA
{
   public  class A
    {
        protected int x = 1;
        private int y;
    }
    public class B : A
    {
        B()
        {
            x = 2; // can access x, but cannot access y
            A a = new A();
            a.x = 1; // Error: Cannot access protected member 'WFA.A.x' via a qualifier of type 'WFA.A'; the qualifier must be of type 'WFA.B' (or derived from it)
        }
    }