access modifier scope

Posted by Gayathri under C# on 12/12/2011 | Points: 10 | Views : 950 | Status : [Member] | Replies : 1
I have a very basic doubt here..
protected - can be accessed only by the dervied classes
protected internal - can be accessed across assembly
internal - can be accessed only by the dervied classes with in the same assembly.
I even tried a program which works as per the above statements. still i want to make sure that i am not wrong..


Is the above correct?
please clarify.
Here is the code.

assembly1
--------------------
using System;

namespace Console1
{
public class A
{
protected int a1;
protected internal int a2;
internal int a3;
protected void A1()
{
a1 = 100;
Console.WriteLine("protected a1");
Console.WriteLine(GetType());
}
protected internal void A2()
{
a2 = 1000;
Console.WriteLine("protected internal a2");
Console.WriteLine(GetType());
}
internal void A3()
{
a3 = 1200;
Console.WriteLine("Internal a3");
Console.WriteLine(GetType());
}
}
public class B : A
{

public void F()
{
Console.WriteLine(a1);
A1();
A2();
A3();
Console.WriteLine(a2);
Console.WriteLine(a3);

}


}
class Test
{
static void Main()
{
A aa = new A();
aa.A2();
aa.A3();

Console.ReadLine();


}
}
}

Assembly2
----------------

using System;
using Console1;


namespace Console2
{
class AA : Console1.A
{

public void f()
{
A1();
A2();


}

}
class fix
{
static void Main()
{
AA atest = new AA();
atest.f();
Console.ReadLine();

}
}




Responses

Posted by: Hmanjarawala on: 12/13/2011 [Member] Bronze | Points: 25

Up
0
Down
you are little bit confuse between internal and protected internal

protected - can be accessed only by the dervied classes
protected internal - can be accessed globaly in assembly but only in derived classes outside assembly
internal - can be accessed globaly with in the same assembly.

means internal variables are like public variable in same assembly.

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Gayathri, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response