Working with .NET access modifiers
Controlling accessAccess 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 levelsThere are five levels of access allowed in .NET. The first access level provides access to everyone with no restrictions. This is called public, so any classes, methods, or properties defined as public are visible to all other code. The classes for ASP.NET pages are public since they need to be accessed to render the page.
The exact opposite of this public access is private, which limits access to its own. Properties or methods defined as private are only accessible by code contained within the same class. Private elements may not be accessed by derived classes or other classes.
There are a few other access levels between the public and private spectrum. The protected access level says that properties and methods defined as protected are visible within its own class and any classes derived from it.
The next level is called a friend in VB.NET and internal in C#. It says properties and methods defined as a friend or internal are accessible to all code by any class within its containing assembly. This level may be combined with the protected access level to define properties and methods that are accessible by all derived classes, as well as any classes within its assembly. This is commonly called ?protected or friend/internal? since it supports either approach.
These five access modifiers are supported by certain VB.NET and C# keywords as the following sections outline.
KeywordsBoth C# and VB.NET includes optional keywords that correspond to the five access levels. The following list provides an overview of these keywords as used in C#:
public: No access restrictions.
protected: Access limited to containing/derived classes.
internal: Access limited to containing type.
protected internal: Access limited to the current project.
private: Access limited to containing/derived classes and the current project.
The corresponding VB.NET list follows:
protected: Access limited to containing/derived classes.
internal: Access limited to containing type.
protected internal: Access limited to the current project.
private: Access limited to containing/derived classes and the current project.
The corresponding VB.NET list follows:
Public: No access restrictions.
Protected: Access limited to containing/derived classes.
Private: Access limited to containing type.
Friend: Access limited to the current project.
Protected Friend: Access limited to containing/derived classes and the current project.
These access modifiers are placed before its associated type. The following VB.NET provides a rudimentary example of the access levels in use. The overall Namespace does not get an access level (it never does) but the class does. The AssemblyOnly class is defined as a Friend, so all code within the assembly may use it. The Function called Test is Public, so it is available for use by everyone within the assembly since its containing class is defined as Friend.
Protected: Access limited to containing/derived classes.
Private: Access limited to containing type.
Friend: Access limited to the current project.
Protected Friend: Access limited to containing/derived classes and the current project.
These access modifiers are placed before its associated type. The following VB.NET provides a rudimentary example of the access levels in use. The overall Namespace does not get an access level (it never does) but the class does. The AssemblyOnly class is defined as a Friend, so all code within the assembly may use it. The Function called Test is Public, so it is available for use by everyone within the assembly since its containing class is defined as Friend.
The testField is set as Private, so it is only accessible within its class. Access to the testField is controlled through properties defined as Public, so it is accessible to all within the assembly since the containing class is defined as Friend. The ChildClassMayExtend method is defined as Protected, so it is accessible in derived classes with these derived classes.
Namespace Example
Friend Class AssemblyOnly
Public Function Test
Return “Test”
End Function
End Class
Public Class EverybodyUse
Private testField As String
Public Sub New
End Sub
Public Property TestProperty() As String
Get
Return testField
End Get
Set(ByVal value As String)
testField = value
End Set
End Property
Protected Overridable Sub ChildClassMayExtend()
’ Code here
End Sub
End Class
End NamespaceThe equivalent C# code follows:
Friend Class AssemblyOnly
Public Function Test
Return “Test”
End Function
End Class
Public Class EverybodyUse
Private testField As String
Public Sub New
End Sub
Public Property TestProperty() As String
Get
Return testField
End Get
Set(ByVal value As String)
testField = value
End Set
End Property
Protected Overridable Sub ChildClassMayExtend()
’ Code here
End Sub
End Class
End NamespaceThe equivalent C# code follows:
namespace test {
internal class AssemblyOnly {
public string test() {
return “test”;
}
public class EverybodyUse {
public EverybodyUse() { }
private string testField;
public string TestField {
get { return testField; }
set { testField = value; }
}
internal virtual void ChildClassMayExtend(){
} } } }Default accessA default access level is used if no access modifier is specified in a member declaration. The following list defines the default access modifier for certain C# types:
internal class AssemblyOnly {
public string test() {
return “test”;
}
public class EverybodyUse {
public EverybodyUse() { }
private string testField;
public string TestField {
get { return testField; }
set { testField = value; }
}
internal virtual void ChildClassMayExtend(){
} } } }Default accessA default access level is used if no access modifier is specified in a member declaration. The following list defines the default access modifier for certain C# types:
enum: The default and only access modifier supported is public.
class: The default access for a class is private. It may be explicitly defined using any of the access modifiers.
interface: The default and only access modifier supported is public.
struct: The default access is private with public and internal supported as well.
The default access may suffice for a given situation, but you should specify the access modifier you want to use to ensure proper application behavior.
class: The default access for a class is private. It may be explicitly defined using any of the access modifiers.
interface: The default and only access modifier supported is public.
struct: The default access is private with public and internal supported as well.
The default access may suffice for a given situation, but you should specify the access modifier you want to use to ensure proper application behavior.
More controlProper object-oriented development involves encapsulation that hides functionality from those who don?t need to see it. The access modifiers available in .NET allow you to put encapsulation in action by defining who can use classes, methods, properties, and so forth, as well as keeping out those who don?t need them. The five access levels available in both C# and VB.NET provide enough control to cover every design situation.
Have you embraced object-oriented development with the .NET Framework? Share your thoughts and experiences with the .NET community.
Tony Patton began his professional career as an application developer earning Java, VB, Lotus, and XML certifications to bolster his knowledge.