What is Access Specifiers in C#?
Access Specifiers defines the scope of a class member. A class member can be variable or function. In C# there are five types of access specifiers are available.
1.Public Access Specifiers
2.Private Access Specifiers
3.Protected Access Specifiers
4.Internal Access Specifiers
5.Protected Internal Access Specifiers.
2.Private Access Specifiers
3.Protected Access Specifiers
4.Internal Access Specifiers
5.Protected Internal Access Specifiers.
The class member, that is defined as public can be accessed by other class member that is initialized outside the class. A public member can be accessed from anywhere even outside the namespace.
Example:
using System;
namespace Public_Access_Specifiers
{
class access
{
// String Variable declared as public
public string name;
// Public method
public void print()
{
Console.WriteLine("\nMy name is "+name);
}
}
class Program
{
static void Main(string[] args)
{
access ac = new access();
Console.Write("Enter your name:\t");
// Accepting value in public variable that is outside the class
ac.name = Console.ReadLine();
ac.print();
Console.ReadLine();
}
}
}
The private access specifiers restrict the member variable or function to be called outside from the parent class. A private function or variable cannot be called outside from the same class. It hides its member variable and method from other class and methods. However, you can store or retrieve value from private access modifiers using get set property. You will learn more about get set property in lateral chapter.
Example:
using System;
namespace Private_Access_Specifiers
{
class access
{
// String Variable declared as private
private string name;
public void print() // public method
{
Console.WriteLine("\nMy name is " + name);
}
}
class Program
{
static void Main(string[] args)
{
access ac = new access();
Console.Write("Enter your name:\t");
// raise error because of its protection level
ac.name = Console.ReadLine();
ac.print();
Console.ReadLine();
}
}
}
The protected access specifier hides its member variables and functions from other classes and objects. This type of variable or function can only be accessed in child class. It becomes very important while implementing inheritance.
Example:
using System;
namespace Protected_Specifier
{
class access
{
// String Variable declared as protected
protected string name;
public void print()
{
Console.WriteLine("\nMy name is " + name);
}
}
class Program
{
static void Main(string[] args)
{
access ac = new access();
Console.Write("Enter your name:\t");
// raise error because of its protection level
ac.name = Console.ReadLine();
ac.print();
Console.ReadLine();
}
}
}
namespace Protected_Specifier
{
class access
{
// String Variable declared as protected
protected string name;
public void print()
{
Console.WriteLine("\nMy name is " + name);
}
}
class Program
{
static void Main(string[] args)
{
access ac = new access();
Console.Write("Enter your name:\t");
// raise error because of its protection level
ac.name = Console.ReadLine();
ac.print();
Console.ReadLine();
}
}
}
The internal access specifier hides its member variables and methods from other classes and objects, that is resides in other namespace. The variable or classes that are declared with internal can be access by any member within application. It is the default access specifiers for a class in C# programming.
Example:
using System;
namespace Internal_Access_Specifier
{
class access
{
// String Variable declared as internal
internal string name;
public void print()
{
Console.WriteLine("\nMy name is " + name);
}
}
class Program
{
static void Main(string[] args)
{
access ac = new access();
Console.Write("Enter your name:\t");
// Accepting value in internal variable
ac.name = Console.ReadLine();
ac.print();
Console.ReadLine();
}
}
}
namespace Internal_Access_Specifier
{
class access
{
// String Variable declared as internal
internal string name;
public void print()
{
Console.WriteLine("\nMy name is " + name);
}
}
class Program
{
static void Main(string[] args)
{
access ac = new access();
Console.Write("Enter your name:\t");
// Accepting value in internal variable
ac.name = Console.ReadLine();
ac.print();
Console.ReadLine();
}
}
}
The protected internal access specifier allows its members to be accessed in derived class, containing class or classes within same application. However, this access specifier rarely used in C# programming but it becomes important while implementing inheritance.
Example:
using System;
namespace Protected_Internal
{
class access
{
// String Variable declared as protected internal
protected internal string name;
public void print()
{
Console.WriteLine("\nMy name is " + name);
}
}
class Program
{
static void Main(string[] args)
{
access ac = new access();
Console.Write("Enter your name:\t");
// Accepting value in protected internal variable
ac.name = Console.ReadLine();
ac.print();
Console.ReadLine();
}
}
}
namespace Protected_Internal
{
class access
{
// String Variable declared as protected internal
protected internal string name;
public void print()
{
Console.WriteLine("\nMy name is " + name);
}
}
class Program
{
static void Main(string[] args)
{
access ac = new access();
Console.Write("Enter your name:\t");
// Accepting value in protected internal variable
ac.name = Console.ReadLine();
ac.print();
Console.ReadLine();
}
}
}
No comments:
Post a Comment