You are on page 1of 10

Access Modifiers

An important aspect of any development platform is security and controlling


access to resources. There are many facets to security, beginning with controlling
application access to defining how code can be used. You can use access
modifiers to define the declared accessibility of code. Here is a rundown of the
various access modifiers available with the .NET Framework.

Controlling access
Access modifiers are an integral part of object-oriented programming.
They support the concept of encapsulation, which promotes the idea
of hiding functionality. Access modifiers allow you to define who does
or doesn’t have access to certain features.
Access Modifiers

C# VB.Net Description

private Private The type or member can be accessed only by


code in the same class or struct.

public Public No access restrictions. The type or member


can be accessed by any other code in the
same assembly or another assembly that
references it.
protected Protected The type or member can be accessed only by
code in the same class or struct, or in a class
that is derived from that class.

internal Friend The type or member can be accessed by any


code in the same assembly, but not from
another assembly.

protected internal Protected Friend The type or member can be accessed by any
code in the assembly in which it is declared,
or from within a derived class in another
assembly.
Access Modifiers
Private (VB.Net) private (C#.Net)
The type or member can be accessed only by code in the same class or struct.

C# VB.Net
Access Modifiers
Public (VB.Net) public (C#.Net)
The type or member can be accessed from anywhere. Public access is the most permissive access level.
There are no restrictions on accessing public members.

C# VB.Net
Access Modifiers
Friend (VB.Net) internal (C#.Net)
Internal members are accessible only within files in the same assembly.

C# VB.Net
Access Modifiers
Protected (VB.Net) protected (C#.Net)
A protected member is accessible from within the class in which it is declared, and from within any
class derived from the class that declared this member.

C# VB.Net
Access Modifiers
Protected Friend (VB.Net) protected internal (C#.Net)
A protected internal member is accessible from within the class /assembly in which it is declared, and
from within any class derived / other assemblies from the class that declared this member.

C# VB.Net
Access Modifiers
Friend Assembly
A friend assembly is an assembly that can access another assembly's Friend
(Visual Basic) or internal (C#) types and members. If you identify an assembly as
a friend assembly, you no longer have to mark types and members as public in
order for them to be accessed by other assemblies.

C# Visual Basic

Usability: During unit testing, when test code runs in a separate assembly but requires access to
members in the assembly being tested that are marked as Friend (Visual Basic) or internal (C#).
Default Access Modifiers
An enum has default modifier as public

A class has default modifiers as Internal . It can declare members (methods etc)
with following access modifiers:
public
internal
private
protected internal

An interface has default modifier as public

A struct has default modifier as Internal and it can declare its members (methods
etc) with following access modifiers:
public
internal
private

A methods, fields, and properties has default access modifier as "Private" if no


modifier is specified.

You might also like