Private access is the least permissive access
level.
Private
members are accessible only within the body of the class or the struct in which
they are declared.
Uses AND Accessibility
1. Cannot be accessed by object
2. Cannot be accessed by derived classes
Example: In the following example Display and FirstName
is not accessible outside the class.
using System;
namespace Modifier
{
class PrivateModifier
{
//private
memebers of class,we can access within class
private string FirstName { get; set;
}
public string LastName { get; set;
}
private void
Display()
{
Console.WriteLine("Public method is
accessible outside class");
}
}
public class
Program
{
public static void
Main()
{
PrivateModifier obj = new PrivateModifier();
//Giving
error when we try to access private member out of type
//error:
inaccessible due to protection level
obj.Display();
//error:
inaccessible due to protection level
obj.FirstName = "abc";
Console.ReadLine();
}
}
}
The above program will give
compilation error (inaccessible due to protection level), as access
to private is not permissible.
nice article
ReplyDelete