Inside a class, any member function can directly access all other members of the same class by name.
Access modifiers are optional prefixes used in member declarations. They control whether other parts of the program can access the member and are written before the type or method name.
Fields
access-modifier type fieldName
Methods
access-modifier return-type methodName()
{
...method body
}Code language: JavaScript (javascript)
Five access levels
privatepublicprotectedinternalprotected internal
For now, readers only need to learn private and public. We will cover the rest in later lessons.
All members, regardless of modifier, are accessible from within the class.
Take a look at this example
class Robot
{
public string name;
int age;
public void SayHello()
{
Console.WriteLine($"Hello everyone my name is {name},I come from Foxdevelop.com");
Console.WriteLine($"I am {age} years old");
}
void AgeUpdate()
{
age = age+1;
}
void SayHi()
{
SayHello();
}
}Code language: C# (cs)

From the example above, you can see that whether it is the public method SayHello, private methods AgeUpdate and SayHi, or the private field age and public field name — everything can access each other as long as the code resides inside the class definition.
One important note: avoid mutual calls, which can trigger infinite recursion. For example
class Robot
{
public string name;
int age;
public void SayHello()
{
Console.WriteLine($"Hello everyone my name is {name},I come from Foxdevelop.com");
Console.WriteLine($"I am {age} years old");
SayHi();
}
void AgeUpdate()
{
age = age+1;
}
void SayHi()
{
SayHello();
}
}Code language: C# (cs)
In this snippet, SayHi calls SayHello, and SayHello in turn calls SayHi. Mutual invocation creates an endless recursive loop.

This will crash the program
You will get an out-of-memory error

Also, you cannot directly call ordinary methods when initializing fields, unless the method is static. Instance methods require an object instance to be invoked, so they cannot be used in field initializers.

class Robot
{
public string name;
int age = GetInt();
static int GetInt()
{
return 1;
}
public void SayHello()
{
Console.WriteLine($"Hello everyone my name is {name},I come from Foxdevelop.com");
Console.WriteLine($"I am {age} years old");
}
void AgeUpdate()
{
age = age+1;
}
void SayHi()
{
SayHello();
}
}Code language: C# (cs)
As shown above, marking the method static makes this valid. Bear in mind that when assigning age this way, if GetInt returns a dynamic value
class Program
{
static void Main(string[] args)
{
Robot robot1 = new Robot();
for (int i = 0; i < 100; i++)
{
robot1.WriteAge();
System.Threading.Thread.Sleep(1000);
}
}
}
class Robot
{
public string name;
int age = GetInt();
static int GetInt()
{
return DateTime.Now.Second;
}
public void WriteAge()
{
Console.WriteLine($"I am {age} years old");
}
}Code language: C# (cs)
In this example, GetInt returns the current second value. The loop inside Main runs 100 times to print age. The program pauses for one second after each output before continuing. You will notice

The printed second value never changes. When age is initialized, GetInt runs once and captures the second value (for example 45). Afterwards age stays fixed at 45 no matter how many times you print it. As mentioned in the previous section, field initial values must be determined at initialization time.
Let’s return to access modifiers
In short: all members are accessible from within the class, regardless of their access modifier.
private Access
Private members can only be accessed inside the class where they are declared. No other class can read or modify them.
Class members use private as the default access level. Omitting the modifier works exactly the same as writing private explicitly; both forms have identical semantics.
int MyInt1; // Implicit private (default)
private int MyInt2; // Explicit privateCode language: C# (cs)
public Access
Public instance members can be accessed by any other object in the program. You must manually add the public modifier to declare them.
public int MyInt; // Marked with public access modifierCode language: C# (cs)
Member Visibility Rules
Class diagrams are commonly used to visualize classes
The whole class is drawn as a box, with members shown as inner compartments;
private members: fully contained inside the class box, invisible externally;
public members: part of the element extends outside the class box, representing global accessibility.
class Program
{
int Member1; // implicit private
private int Member2; // explicit private
public int Member3; // public
}Code language: C# (cs)

Example used in this chapter
class C1
{
int F1; // Implicit private field
private int F2; // Explicit private field
public int F3; // Public field
void DoCalc() // Implicit private method
{
...
}
public int GetVal() // Public method
{
...
}
}Code language: C# (cs)
- Private members (F1, F2, DoCalc()): Only code inside class
C1can read, write and invoke them. External classes cannot see them at all; - Public members (F3, GetVal()): Accessible without restrictions from any other class or object in the program;
- Intra-class rule: All methods inside
C1, public or private, can access every field and method belonging to the same class.
Runable demonstration
class Program
{
static void Main(string[] args)
{
Worker worker = new Worker();
Console.WriteLine(worker.GetHours()); // Valid. Public member GetHours accessible outside class
Console.WriteLine(worker.ReadSavings());// Error. Cannot access private ReadSavings externally, even via instance name worker.
Console.WriteLine(worker.hours); // Valid. Public field hours accessible outside class
Console.WriteLine(worker.savings); // Error. Cannot access private savings externally
Console.WriteLine(worker.age); // Error. Cannot access private age externally
}
}
class Worker
{
int age; // Implicit private field
private double savings; // Explicit private field
public int hours; // Public field
void ReadSavings() // Implicit private method
{
Console.WriteLine(savings);
}
public int GetHours() // Public method
{
return hours;
}
}Code language: C# (cs)

You should now have a basic grasp of access modifiers.
Access Modifiers