Private class in C #

2

I have a class Pessoa.cs private , but I can call it in another class, for example I created a class called Parametro.cs ; I can instantiate the class private Pessoa without the slightest problem. Should not private not allow access to this Pessoa class? At least the logic is when it comes to properties, for example. I did not see difference between private or public in class.

Persona.cs

private class Pessoa
    {
        public string nome { get; set; }
        public int idade { get; set; }
        public decimal peso { get; set; }

    }

Parameter.cs

class Parametro
    {
        public void PreencherPessoa() {
            Pessoa pessoa = new Pessoa();
            pessoa.nome = "José";
            pessoa.idade = 30;
            pessoa.peso = 80;

            MostrarPessoa(pessoa);
        }
    }
    
asked by anonymous 01.06.2018 / 13:16

1 answer

2

private is not a type. It is an access modifier. It is an attribute that determines where that type or member of a type may be visible. But visible only where? In this code type Pessoa is inside what? Should he be deprived of what?

In the case can only be visible inside another class or structure .

But if the class is not inside another class, it can be visible inside everything, that is, private has nothing, just use wrong, so did not see difference. In practice you can do outside the class, but will still be a detail implementation of the other class of the file.

Private classes are used only for internal implementation, never for general use. Only details that are not part of the type agreement can be private.

That is why I always say that you can not learn by seeing the result, but rather you have to learn by seeing the foundation that leads to that result, even to know if it is coincidence or not. Just because it worked this way does not mean it's correct. It's an inglorious fight. Even people who have been through this kind of thing many times, still learn the wrong way.

    
01.06.2018 / 15:19