Chapter 13
Access Levels
Every category member has associate degree accessibility level that determines wherever the member are visible. There are a unit 5 of them on the market in C#: public, protected, internal, and protected internal and personal. The default access level for members of a category is non-public.
Private access
All members in spite of access level area unit accessible within the category during which they're declared, the insertion category. The sole place wherever a non-public member can be accessed.
class MyBase
{
// unrestricted access public int myPublic;
// shaping assembly or derived category protected internal int myProtInt;
// shaping assembly internal int myInternal;
// shaping or derived category protected int myProtected;
// shaping category solely non-public int myPrivate;
void Test()
}
Protected access
A protected member may be accessed from among a derived category, however it's inaccessible from different categories.
class Derived : MyBase
}
Internal access
An internal member will be accessed anyplace among the native assembly, however not from another assembly. In .NET, associate degree assembly is either a program (.exe) or a library (.dll).
// shaping assembly category AnyClass
}
Protected internal access
Protected internal access means that either protected or internal. A protected internal member will so be accessed anyplace among this assembly, or in categories outside the assembly that area unit derived from the insertion category.
// different assembly category Derived: MyBase
{
void Test(MyBase m)
}
Public access
Public access provides unrestricted access from anyplace that the member will be documented.
// different assembly category AnyClass
}
Top-level access levels
A commanding member could be a sort that's declared outside of the other sorts. In C#, the subsequent sorts will be declared on the top-level: category, interface, strict, enum and delegate. By default, these uncontained members area unit given internal access. To be able to use a commanding member from another assembly the members ought to be marked as public. This is often the sole different access level allowed for commanding members.
Internal category MyInternalClass public category MyPublicClass
Inner categories
Classes could contain inner categories, which might be set to either one in every of the 5 access levels. The access levels have an equivalent result on inner categories as they are doing on different members.
If the category is inaccessible, it cannot be instantiated or heritable. By default, inner categories area unit non-public, which implies that they'll solely be used among the category wherever they're outlined.
Class MyBase
{
// Inner categories (nested classes) public class MyPublic
Protected internal category MyProtInt internal category MyInternal protected category MyProtected private class MyPrivate
}
Access level guideline
As a suggestion, once selecting associate degree access level it's usually best to use the foremost restrictive level doable. {This is this is often this will be} as a result of the lot of places a member will be accessed the lot of places it can be accessed incorrectly, that makes the code tougher to rectify. Victimization restrictive access levels will create it easier to switch {the category|the category} while not breaking the code for the other programmer’s victimization that class.