Visual Programming
Visual Programming
That's why when we run an assembly all classes and interfaces inside the
assembly run together.
class Student {
Here,
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
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
using System;
namespace MyApplication {
class Student {
public string name = "Ahmad Hassan";
class Program {
static void Main(string[] args) {
Output
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() {
Console.WriteLine("Hello from Student class");
}
}
class Program {
static void Main(string[] args) {
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.
using System;
namespace MyApplication {
class Student {
protected string name = "Ahmad Hassan";
}
class Program {
static void Main(string[] args) {
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.
using System;
namespace MyApplication {
class Student {
protected string name = "Ahmad Hassan";
}
// derived class
class Program : Student {
static void Main(string[] args) {
Output
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.
using System;
namespace Assembly {
class Student {
internal string name = "Ahmad Hassan";
}
class Program {
static void Main(string[] args) {
Output
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.
// Code on Assembly1
using System;
namespace Assembly1 {
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();
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;
// 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();
Console.WriteLine(greet.msg);
Console.ReadLine();
}
}
}
Output
Hello
// Code on Assembly2
using System;
// access Assembly1
using Assembly1;
namespace Assembly2 {
Output
Hello
// Code in Assembly1
using System;
namespace Assembly1 {
public class StudentName {
private protected string name = "Ahmad Hassan";
}
Output
Ahmad Hassan
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 {
This is because the name field is in Assembly1 and the derived class is
in Assembly2.