0% found this document useful (0 votes)
10 views16 pages

C# Access Modifiers Explained

An assembly is a collection of types and resources that are built to work together to form a logical unit of functionality. The document discusses the different access modifiers in C# - public, private, protected, and internal - and their effects on accessibility and scope. It provides examples of how each access modifier functions within and across class and assembly boundaries.

Uploaded by

hasnainmushtaq23
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views16 pages

C# Access Modifiers Explained

An assembly is a collection of types and resources that are built to work together to form a logical unit of functionality. The document discusses the different access modifiers in C# - public, private, protected, and internal - and their effects on accessibility and scope. It provides examples of how each access modifier functions within and across class and assembly boundaries.

Uploaded by

hasnainmushtaq23
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

What is Assembly

An assembly is a collection of types (classes, interfaces, etc) and resources


(data). They are built to work together and form a logical unit of functionality.

That's why when we run an assembly all classes and interfaces inside the
assembly run together.

C# Access Modifiers / Specifiers


C# Access modifiers or specifiers are the keywords that are used to specify
accessibility or scope of variables and functions in the C# application.

class Student {

public string name;

private int num;

Here,

• name - public field that can be accessed from anywhere


• num - private field can only be accessed within the Student class
Types of Access Modifiers
In C#, there are 4 basic types of access modifiers.

1. public

2. private

3. protected

4. internal
We can choose any of these to protect our data. Public is not restricted and Private is
most restricted. The following table describes about the accessibility of each.
Access Description
Specifier

Public It specifies that access is not restricted.

Protected It specifies that access is limited to the containing class or in derived


class.

Internal It specifies that access is limited to the current assembly.

Private It specifies that access is limited to the containing type(class).

protected it can be accessed from the same assembly and the derived class of the
internal containing class from any other assembly.

Private it can only be accessed within the same class, and its derived class within
protected the same assembly

Now, let's create examples to check accessibility of each access specifier.


1. Public Access Modifier
When we declare a type or type member public , it can be accessed from
anywhere. For example,

using System;

namespace MyApplication {

class Student {
public string name = "Ahmad Hassan";

public void print() {


[Link]("Hello from Student class");
}
}

class Program {
static void Main(string[] args) {

// creating object of Student class


Student student1 = new Student();

// accessing name field and printing it


[Link]("Name: " + [Link]);

// accessing print method from Student


[Link]();
[Link]();
}
}
}

Output

Name: Ahmad Hassan


Hello from Student class

In the above example, we have created a class named Student with a


field name and a method print() .
// accessing name field and printing it
[Link]("Name: " + [Link]);

// accessing print method from Student


[Link]();

Since the field and method are public, we are able to access them from
the Program class.
2. Private Access Modifier
When we declare a type member with the private access modifier, it can only
be accessed within the same class or struct . For example,

using System;

namespace MyApplication {

class Student {
private string name = "Ahmad Hassan";
private void print() {
[Link]("Hello from Student class");
}
}

class Program {
static void Main(string[] args) {

// creating object of Student class


Student student1 = new Student();

// accessing name field and printing it


[Link]("Name: " + [Link]);

// accessing print method from Student


[Link]();
[Link]();
}
}
}
In the above example, we have created a class named Student with a
field name and a method print() .

// accessing name field and printing it


[Link]("Name: " + [Link]);

// accessing print method from Student


[Link]();

Since the field and method are private, we are not able to access them from
the Program class. Here, the code will generate the following error.

Error CS0122 '[Link]' is inaccessible due to its protection level


Error CS0122 '[Link]()' is inaccessible due to its protection level
3. Protected Access Modifier
When we declare a type member as protected , it can only be accessed from
the same class and its derived classes. For example,

using System;

namespace MyApplication {

class Student {
protected string name = "Ahmad Hassan";
}

class Program {
static void Main(string[] args) {

// creating object of student class


Student student1 = new Student();

// accessing name field and printing it


[Link]("Name: " + [Link]);
[Link]();
}
}
}

In the above example, we have created a class named Student with a field name .

Since the field is protected, we are not able to access it from the Program class.
Here, the code will generate the following error.

Error CS0122 '[Link]' is inaccessible due to its protection level


Now, let's try to access the protected member from a derived class.

using System;

namespace MyApplication {

class Student {
protected string name = "Ahmad Hassan";
}

// derived class
class Program : Student {
static void Main(string[] args) {

// creating object of derived class


Program program = new Program();

// accessing name field and printing it


[Link]("Name: " + [Link]);
[Link]();
}
}
}

Output

Name: Ahmad Hassan

In the above example, we have created a class Student with a protected


field name . Notice that we have inherited the Program class from the Student class.

// accessing name field and printing it


[Link]("Name: " + [Link]);

Since the protected member can be accessed from derived classes, we are
able to access name from the Program class.
4. Internal Access Modifier
When we declare a type or type member as internal , it can be accessed only
within the same assembly.

Example: internal within the same Assembly

using System;

namespace Assembly {

class Student {
internal string name = "Ahmad Hassan";
}

class Program {
static void Main(string[] args) {

// creating object of Student class


Student theStudent = new Student();

// accessing name field and printing it


[Link]("Name: " + [Link]);
[Link]();
}
}
}

Output

Name: Ahmad Hassan

In the above example, we have created a class named Student with a field name .

Since the field is internal , we are able to access it from the Program class as
they are in the same assembly.
If we use internal within a single assembly, it works just like the public access
modifier.

Example: internal in different Assembly

Let's create one assembly first.

// Code on Assembly1
using System;

namespace Assembly1 {

public class StudentName {


internal string name = "Ahmad Hassan";
}

class Program {
static void Main(string[] args) {
}
}
}

Here, this code is in Assembly1. We have created an internal field name inside
the class StudentName . Now, this field can only be accessed from the same
assembly Assembly1.
Now, let's create another assembly.

// Code on Assembly2
using System;

// access Assembly1
using Assembly1;

namespace Assembly2 {
class Program {
static void Main(string[] args) {
StudentName student = new StudentName();

// accessing name field from Assembly1


[Link]([Link]);
[Link]();
}
}
}

Here, this code is in Assembly2. We are trying to access the name field of
the StudentName class(Assembly1).
To access fields from Assembly1, we first need to set the reference
of Assembly1 in Assembly2. Now the code

using Assembly1;

allows us to use the code from Assembly1 to Assembly2.


Here, when we try to access the name field from Assembly2, we get an error.

Error CS0122 '[Link]' is inaccessible due to its protection level

This is because name is an internal field present in Assembly1.


5. Protected Internal Access Modifier
The protected internal is a combination of protected and internal access
modifiers.
When we declare a member protected internal , it can be accessed from the
same assembly and the derived class of the containing class from any other
assembly.

// Code on Assembly1
using System;

namespace Assembly1 {
public class Greet {
protected internal string msg="Hello";
}

class Program {
static void Main(string[] args) {
Greet greet = new Greet();
[Link]([Link]);
[Link]();
}
}
}

Output

Hello

The above code is in Assembly1.


In the above example, we have created a class named Greet with a field msg .

Since the field is protected internal, we are able to access it from


the Program class as they are in the same assembly.
Let's derive a class from Greet in another assembly and try to access the
protected internal field msg from it.

// Code on Assembly2
using System;

// access Assembly1
using Assembly1;

namespace Assembly2 {

// derived class of Greet


class Program: Greet {
static void Main(string[] args) {
Program greet = new Program();

// accessing name field from Assembly1


[Link]([Link]);
[Link]();
}
}
}

Output

Hello

The above code is in Assembly2.


In the above example, we have inherited the Program class from
the Greet class(from Assembly1).

// accessing name field from Assembly1


[Link]([Link]);

We are able to access the msg from the Greet class


of Assembly1 from Assembly2.
This is because the msg is a protected internal field and we are trying to access
it from the child class of Greet .
6. Private Protected Access Modifier
The private protected access modifier is a combination of private and protected . It
is available from the C# version 7.2 and later.
When we declare a member private protected , it can only be accessed within
the same class, and its derived class within the same assembly. For example,

// Code in Assembly1
using System;

namespace Assembly1 {
public class StudentName {
private protected string name = "Ahmad Hassan";
}

//derived class of StudentName class


class Program1 : StudentName {

static void Main(string[] args) {

Program1 student = new Program1();

// accessing name field from base class


[Link]([Link]);
[Link]();
}
}
}

Output

Ahmad Hassan

The above code is in Assembly1


In the above example, we have created a class StudentName with a private

protected field name .

Notice that we have inherited the Program1 class from the StudentName class.
Since the private protected member can be accessed from derived classes
within the same assembly, we are able to access name from the Program1 class.
Let's derive a class from StudentName in another assembly and try to access the
private protected field name from it. For example,

// Code in Assembly2
using System;
//access Assembly1
using Assembly1;

namespace Assembly2 {

//derived class of StudentName


class Program : StudentName {
static void Main(string[] args) {
Program student = new Program();

// accessing name field from Assembly1


[Link]([Link]);
[Link]();
}
}
}

The above code is in Assembly2


In the above example, when we try to access the name field from the derived
class of StudentName , we get an error.

Error CS0122 '[Link]' is inaccessible due to its protection level

This is because the name field is in Assembly1 and the derived class is
in Assembly2.

Common questions

Powered by AI

The 'private protected' access modifier is useful for allowing access to members within the same assembly, specifically within the containing class and any derived classes. However, it cannot be accessed outside the assembly, even in derived classes, adding a layer of security and encapsulation .

The 'protected internal' modifier allows access within the same assembly and by derived classes in other assemblies. For instance, a class Greet in Assembly1 with a 'protected internal' field msg can be accessed directly in the same assembly and by a derived class Program in Assembly2 .

Assemblies in C# provide a logical grouping of functionalities, affecting access control by determining the scope from which certain members (e.g., internal) can be accessed. Members marked as internal become accessible only within the same assembly, reinforcing encapsulation principles .

The 'internal' access modifier restricts the accessibility of a type or member to only within its own assembly, whereas 'public' allows access from any assembly and 'private' restricts access to only within the containing type (class).

A member marked with the 'protected' modifier is accessible in derived classes but not in non-derived classes. For instance, a protected field in a base class is accessible in a derived class, yet attempting access from a non-derived class will result in an access level error .

You would encounter an error stating the field is inaccessible due to its protection level because internal fields are only accessible within the assembly they are declared unless appropriately referenced .

To use an internal member from another assembly, one must reference the assembly in which the member was originally declared. However, this approach can lead to potential drawbacks like increased dependency between assemblies, potentially complicating maintenance and future changes .

An assembly in C# serves as a collection of types (such as classes and interfaces) and resources (like data) that together form a logical unit of functionality. When an assembly is executed, all its classes and interfaces work together .

Encapsulation in C# is the principle of bundling the data (variables) and methods that operate on the data into a single unit or class, and controlling access to this data through access modifiers. Access modifiers like public, private, protected, internal, protected internal, and private protected restrict or permit accessibility to various degrees, helping to maintain integrity and prevent unintended interference or misuse of the data .

An error occurs because private members are accessible only within their own class. Attempting to access them from an external class or method violates this restriction, leading to access level errors .

You might also like