Thursday, February 5, 2009

Accessibility Levels (C# Reference)

Considering C#.NET to define the access specifiers at different levels of accessing a class or its members.







Access specifierMeaning
publicA class or its members declared as public are not restricted and can be accessed at any instant
private A class or its members declared as private are restricted such that they can be accessed only locally or in same class
internal A class or its members declared as internal are restricted such that they can be accessed only in there specific assembly in which it is defined
protected internal A class or its members declared as protected internal are restricted such that they can be accessed only in there specific assembly in which it is defined and also when class is extended or derived
protected A class or its members declared as protected are restricted such that they can be accessed only when class is extended or derived


At a particular instant we can use only one access specifier, where as it could be “protected internal”.
Example:-
1) public Example
public class Demo1
{
public void Hello()
{
}
}

This mean that this class “Demo1” can be called in any class which may belong to even different assembly and also its public member “Hello()”
2) private Example
public class Demo1
{
private void Hello()
{
}
public void Tell()
{
Hello();
}
}

This mean that this class “Demo1” can be called in any class which may belong to even different assembly but cannot call or refer its private member “Hello()”.
Whereas “Tell” public member can be called which indirectly calls the private method “Hello()” in the given example.
3) internal example


namespace test
{
public class Demo1
{
internal void Hello()
{
}
public void Tell()
{
Hello();
}
}
}

This mean that this class “Demo1” can be called in any class which may belong to even different assembly but cannot call or refer its internal member “Hello()” unless the calling method or member or class belongs to same assembly (test).
Where as “Tell” public member can be called which indirectly calls the internal method “Hello()” in the given example.
4) protected internal Example


namespace test
{
public class Demo1
{
protected internal void Hello()
{
}
public void Tell()
{
Hello();
}
}
}

This mean that this class “Demo1” can be called in any class which may belong to even different assembly but cannot call or refer its protected internal member “Hello()” unless the calling method or member or class belongs to same assembly (test) and Class Demo1 is extended or derived from this class in which we are willing to call this internal protected member “Hello()”..
Where as “Tell” public member can be called which indirectly calls the protected internal method “Hello()” in the given example.

5) protected Example


namespace test
{
public class Demo1
{
protected void Hello()
{
}
public void Tell()
{
Hello();
}
}
}

This mean that this class “Demo1” can be called in any class which may belong to even different assembly but cannot call or refer its protected member “Hello()” unless Class Demo1 is extended or derived from this class in which we are willing to call this protected member “Hello()”..
Where as “Tell” public member can be called which indirectly calls the protected internal method “Hello()” in the given example.

No comments: