public
and private
are one of the many access modifiers used to highlight the context of the object you are defining.
An access modifier is one that is responsible for controlling the properties or methods that you can access in an object, consider the following example:
public class MiObjeto
{
private int _X;
public int X { get { return _X; } }
publix void SetX(int x)
{
_X = x;
}
}
When creating a new instance of MiObjeto
, you define a copy of the class, in which you can use its public members to get p to adjust its value as necessary; when calling the SetX(5)
method the new value of the private variable _X
is 5, because a private member is accessed from a public member.
Private members are those who can only be accessed from within the class, that is, any method or function that is public or private, has access to the other properties of these areas.
With the previous code, if in Main
I do:
MiObjeto Prueba = new MiObjeto();
Console.WriteLine(Prueba._X); // ERROR!
Console.WriteLine(Prueba.X); // Perfecto, imprime cero
Prueba.SetX(10);
Console.WriteLine(Prueba.X); // Imprime 10.
In summary, the access modifiers control the methods, the fields and properties that you let the end user can use, and the behavior that they can handle using your object.
EDIT:
With respect to the static
modifier, its behavior honors its name, means "static", means it does not move, it is always available, so you do not need a reference to call a method of this type .
The access modifiers public
and private
can be mixed with static
but not between themselves:
private static int MiMiembro; // Bien
public static int OtroMiembro; // Perfecto
static char Caracter; // Todo bien
public private int KHE; //ERROR!
To which in addition I mention, there are certain points where something called "Accessibility Inconsistency" is presented, this happens when you assign an access modifier that is not compatible with any member of your class.