Online Training for Asp.net MVC Web Designing Development, MS SQL, Digital Marketing

Access modifiers in C# .Net Tutorials


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

Access modifiers in C#

private indicate the accessibility is limited only inside the classes / structure
public indicates, it can be accessed from anywhere that means there is no restriction on accessibility
protected Indicates the accessibility is limited within the class and any class :Inherited from this class
internal Internal access modifiers indicate that Access is limited exclusively to classes defined within the current project assembly
protected internal 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

private access modifier

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

protected access modifier

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
internal access modifier

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.

protected internal access modifier

Hope you have understood C#




 
Access modifiers
in C# Tutorials
Learn C# Online Tutorial


Group Training