All access modifiers in C# and Keywords in C# for anyone who wants to learn C# Programming software engineering student, trainee, c# developer, programmer or working professional
indicate the accessibility is limited only inside the classes / structure
indicates, it can be accessed from anywhere that means there is no restriction on accessibility
Indicates the accessibility is limited within the class and any class :Inherited from this class
Internal access modifiers indicate that Access is limited exclusively to classes defined within the current project assembly
Access is limited to the current assembly and types derived
Now let's experiment how above access modifiers in C# works
We have created following class with five different properties with different modifiers, will test them one by one
class ModifierKeywordSample
{
public string WTRPublic { get; set; }
private string WTRPrivate { get; set; }
protected string WTRProtected { get; set; }
internal string WTRInternal { get; set; }
protected internal string WTRProtectedInternal { get; set; }
}
Test 1: Here is the test result of private modifier,
If you notice, we can’t see the private member in image below
Test 2: test result of protected modifier, we can’t see the private protected in image below
Note: in earlier example protected member was appearing, because "ModifierKeywordSample" class was inherited
Test 3: test result of internal modifier, we can’t see the internal protected in image below
Now to test “Internal” we have created a new application and then added the reference of earlier application, then inherited from “ModifierKeywordSample” which is in other assembly
Test 4: test result of protected internal modifier, Now instead of inheriting from "ModifierKeywordSample" class, if we create a new instance of same "ModifierKeywordSample" , we can't see protected internal member anymore.
Hope you are enjoying C# Programming