Free Web Development Online Tutorials, Learn to Code
Access Modifiers in C#, access modifiers available in c#, c# classes and access modifiers

C# Access Modifiers Examples

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# .Net

  • 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; }
}

private access modifier in c#

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

protected access modifier in c#

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

internal access modifier in c#

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

protected internal access modifier in c#

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 are enjoying C# Programming


Comment
Name
Email
Website
Subscribe
 

Access modifiers
in C# Tutorials
Other Online Tutorial

C# .net Interview Questions Answers