C # Access to private methods

0

First I want to exemplify with another topic, before touching the main topic ... reading the theoretical material that I have, if I have a class member such as

class Prueba
{
    public static void ejemplo()
{
Console.WriteLine("soy un ejemplo")
}
}

My material says that a class member can only be called from the same class and not from the created object instance, this is correct

Prueba.ejemplo() >>>>>>>>> output "soy un ejemplo"

Now the main topic ... I kept reading this topic and I crossed with the modifiers public and private and read exactly the same, that is to say that if I have a private method of a class this can only be accessed from the same class, but how is the syntax for this ... why I was trying combinations and did not hit anything.

    
asked by Shiki 09.05.2017 в 04:49
source

2 answers

3

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.

    
answered by 09.05.2017 / 05:45
source
0

I do not think it's a good practice what you intend to do. For that they are private so that they can not be accessed and used for example to encapsulate some logic that you want to hide and / or reuse.

See if it is so that even if you access them through the System.Reflection library, you will not be able to access public methods or properties. The access modifiers are for controlling those things: public, private, protected and internal.

    
answered by 09.05.2017 в 12:18