Tuesday, March 12, 2013

Access Specifiers in .NET

Learn C# Acess specifiers
Access Specifiers

An access specifier defines the scope of a class member. A class member refers to the variables and functions in a class. A program can have many classes. The programmer gets the privilege to decide the of use access specifier to implement encapsulation and abstraction in C#.
C# supports five types of access specifiers
They are:-


.Public
.Private
Protected
internal
protected internal.

public:
The public access specifier in C# allows a class to expose its member variables and member functions to other functions and objects. So any member declared public can be accessed outside the class also.

Example:

using System;
namespace
HelloWorld
{
class hello
{
public
int iNum1;
}
class
MyMainClass
{
static void Main(string[]
args)
{
hello
HelloObject=new hello();
HelloObject.iNum1=10; /* since variable iNum1 is public it can be accessed in other classes also*/

Console.WriteLine(HelloObject.iNum1);
} } }

Private:

The private access specifier in C# is just opposite to the public access specifier. That is it allows a class to hide its member variables and member functions from other class objects and functions. So it is not visible outside the class. By default, the access specifier is private; if public, private or protected is not specified.



Example

using System;
namespace HelloWorld
{
class hello
{
public int iNum1;
private int iNum2;

public hello()
{
iNum1=0;
iNum2=10;
}
}
class MyMainClass
{
static void Main(string[] args)
{

hello HelloObject=new hello();
//CORRECT METHOD
HelloObject.iNum1=10;
 //Here since variable iNum1 is public it can be accessed in other classes also

//WRONG METHOD
HelloObject.iNum2=20; /*This line will return an Error since the access to this variable is Private. So it cannot be accessed outside the class*/

Console.WriteLine(HelloObject.iNum1);
}
}
}



protected:
The protected access specifier in C# allows a class to hide its member variables and member functions from other class objects and functions, except the child class. This access specifier is used when we need to use Inheritance in the program.

Example

using System;
namespace HelloWorld
{
class hello
{
public int iNum1;
protected int iNum2;
}

class world : hello
{
public int AddDetails()
{
iNum1=20;
iNum2=10;

return iNum1+iNum2;
}
}

        class MyMainClass
{
static void Main(string[] args)
{
world worldObject=new world();

worldObject.iNum1=50; //Line 1 No Error

worldObject.iNum2=10; //Line 2 Error

Console.WriteLine(worldObject.AddDetails().ToString());

}
}
}

internal:


The internal access specifier in C# allows a class to expose its member variables and member functions to other function and objects. It can be accessed from any class or method defined with the application in which the member is defined. The default access specifier is internal for a class.


protected internal:

The protected internal access specifier in C# allows methods or member variables accessible to a class and its derived classes inside the same assembly or namespace within a file. These members cannot be accessed from class inside another assembly or a file.

Example

using System;
class Student
{
private string sAddress;
protected string sPhNo;
protected internal long iZipCode;
void Hello()
{
Console.WriteLine(“Hello World!”);
}
internal void Hello2()
{
Console.WriteLine(“Hello Student!”);
}
public void SetAddress()
{
Console.WriteLine(“Enter Address:”);
sAddress = Console.ReadLine();
}
public void SetPhNo()
{
Console.WriteLine(“Enter your Phone Number:”)
sPhNo = Console.ReadLine();
}
public void SetZipCode()
{
Console.WriteLine(“Enter the Zip Code: “);
iZipCode =Convert.ToInt64(Console.ReadLine());
}
public void DisplayStudent()
{
Console.WriteLine(“The Address is: {0}”, sAddress
);
}
}

class Display
{
static void Main(string[] args)
{
Student John = new Student();

Console.WriteLine(John.sAddress); //Error: protected members cannot be accessed 

John.SetAddress(); //public members can be accessed outside the class definition 

John.SetPhNo(); //public members can be accessed outside the class definition 

Console.WriteLine(John.sPhNo); //error: protected internal members cannot be accessed outside the class definition 

John.SetZipCode(); //public members can be accessed outside the class definition 

John.DisplayStudent(); // public members can be accessed outside the class definition 

Console.WriteLine(John.sAddress); //error: private members cannot be accessed outside the class definition

John.Hello(); //error: private members cannot be accessed outside the class definition 

John.Hello2(); //displays Hello student!

Console.ReadLine();
}
}




0 comments:

Post a Comment