اگر فیلدی را با اصلاح کننده دسترسی خصوصی اعلام کنید، فقط می تواند
در همان کلاس قابل دسترسی است:
مثال
class Car
{
private string model;
static void Main(string[] args)
{
Car Ford = new Car("Mustang");
Console.WriteLine(Ford.model);
}
}
The output will be:
Mustang
اگر سعی کنید خارج از کلاس به آن دسترسی داشته باشید، خطایی رخ می دهد:
مثال
class Car
{
private string model = "Mustang";
}
class Program
{
static void Main(string[] args)
{
Car myObj = new Car();
Console.WriteLine(myObj.model);
}
}
The output will be:
'Car.model' is inaccessible due to its protection level
The field 'Car.model' is assigned but its value is never used