0

This is an example from MSDN which is from the portion explaining 'protected'member access modifier. My question is, why I am getting compile error, if I modifying this program like in Example II,

Example I

class A
{
    protected int x = 123;
}

class B : A
{
    static void Main()
    {
        A a = new A();
        B b = new B();

        b.x = 10;
    }
}

Example II

class A
{
    protected int x = 123;
}

//MODIFICATION IN BELOW 2 LINES
class B : A{}
class program
{
    static void Main()
    {
        A a = new A();
        B b = new B();

        b.x = 10;
    }
}

Compiler Error for Example II :

d:\MyProgs>csc _13protected.cs
Microsoft (R) Visual C# 2010 Compiler version 4.0.30319.1
Copyright (C) Microsoft Corporation. All rights reserved.

_13protected.cs(14,15): error CS0122: 'A.x' is inaccessible due to its
        protection level
_13protected.cs(3,23): (Location of symbol related to previous error)

d:\MyProgs>
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Arun
  • 1,651
  • 4
  • 20
  • 31
  • Isn't it explained on the exact same place you got the code [snippet](http://msdn.microsoft.com/en-us/library/bcd5672a.aspx) from? – Montaldo Jun 25 '14 at 08:01
  • 1
    And it is said there. That `protected` protects the variable to be only used by it's own and derived classes. `class program` is neither of those, thus you cannot access the variable `x` from the `class program`. And thus you get an appropriate compile error. – Montaldo Jun 25 '14 at 08:04

3 Answers3

4

protected means it's not visible outside of the class itself, only in the class itself or derived classes.

In your first example it works because your main method is part of the derived class.

In your second example, your are trying to access a protected member outside of its class, which is not possible. If you want to make this possible, x should be declared public.

See http://msdn.microsoft.com/en-us/library/bcd5672a.aspx for more information about what protected means.

L-Four
  • 13,345
  • 9
  • 65
  • 109
3

Read the definition of access modifiers and you will get your answer.

public: The type or member can be accessed by any other code in the same assembly or another assembly that references it.

private: The type or member can be accessed only by code in the same class or struct.

protected: The type or member can be accessed only by code in the same class or struct, or in a class that is derived from that class.

internal: The type or member can be accessed by any code in the same assembly, but not from another assembly.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
Ricky
  • 2,323
  • 6
  • 22
  • 22
0

You need to pay attention to actual classes where access to protected field happens.

In second example you are trying to access b.X from the program class, which is not allowed by "protected" modifier, as program class doesn't inherit from A.

On the other hand, In first example actuall access to b.X happened in B class, which inherits from A, therefore access is allowed by "protected" modifier.

user3613916
  • 232
  • 1
  • 10