Tuesday, October 26, 2010

understanding the protected internal Access Specifier in c#:

The protected internal access Specifier allows a class to show its member variables and member functions to the containing class, derived classes, or to classes within the same application. The protected internal access specifier becomes important while implementing inheritance.

The following example shows that the protected internal member of the class will have the same visibility as a private member of the class.

using System;
class Car
{
protected internal string Model;
void Honk()
{
Console.WriteLine("PARRP PARRP!");
}
public void SetModel()
{
Console.WriteLine("Enter the model name:");
Model = Console.ReadLine();
}
public void DisplayModel()
{
Console.WriteLine("The model is:(0)", Model);
}
}
class Display
{
static void Main(string[] args)
{
Car Ford = new Car();
Ford.SetModel(); // Accepts the model name
Ford.DisplayModel(); //Display the model name
Console.ReadLine();

// Ford.Honk(); error! private members Cannot be Accessed outside the class definations */
// Console.WriteLine("Ford.Model"); error! protected internal members Cannot be Accessed outside the class defination /*



}
}

Note:- The SetModel () and DisplayModel () functions can be called from the Ford object defind in the Main () function. However, the Model variable cannot be accessed through the Ford object because it is a protected internal member variable. Similarly, the Honk () function cannot be accessed through the Ford object because it is also a private member function.


C Sharp Access specifier Related Contents.

Understanding the public Access specifier in c#:
Understanding The private access specifier in c Sharp.
Understanding The protected access specifier in c Sharp.
Understanding The internal access specifier in c Sharp.
if...else construct in c sharp.

No comments:

Recent Posts