Friday, October 22, 2010

Understanding The internal access specifier in c Sharp.

The internal access specifier allows a class to expose its member variables and member functions to other functions and objects। Any member that is declared internal can be accessed from any class or method defined within the application in which the member is defined. The default access specifier for a class is internal.

The following is an example of the use of the internal access specifier:

using System;
class Car
{
private string CarColor; //since the variable is private, it cannot be accessed outside the class defination.
internal void Honk()
{
Console.WriteLine("BEEP BEEP!");
}
}
class Bike
{
internal string BikeColor; // since the variable is internal, it can be accessed outside the class defination.
}
class Result
{
static void Main(string[] args)
{
Car Ford = new Car();
Bike Honda = new Bike();
//Ford.CarColor = "red"; //Error! cannot access private members.

Honda.BikeColor = "Blue";
Ford.Honk(); // Displays BEEP BEEP!
Console.ReadLine();
}
}


Note: The CarColor variable cannot be accessed from any function outside the car class. On the other hand, the BikeColor variable is an internal member. Hence, it can be accessed from outside the class. Also, the Honk() function will be accessible from anywhere in the program and can be accessed by using the objects of the Car class.


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 protected internal Access Specifier in c#:
if...else construct in c sharp.

No comments:

Recent Posts