Private class in C#

Asked

Viewed 244 times

2

I have a class Pessoa.cs private, but I can call her in some other class, for example I created a class called Parametro.cs; I can instantiate the class private Pessoa without the slightest problem. The private should not allow access to that class Pessoa? At least the logic is this when it comes to properties, for example. I saw no difference between private or public in class.

Pessoa.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);
        }
    }
  • I did this just to test the private access modifier in the class. The Parameter class does not inherit from Person.

  • 2

    @Heyjoe, just confirm one thing, the two classes are in the same scope right? They are both in the same Project. If you try to call the class "Person" out of the project you are in, it will give error, right?

  • ah, so the private only serves if it is outside the project? I took the test here.

  • 2

    You can create the Person class within Parameter, as both were created in the same file. Create both in separate files and you will no longer be able to instantiate.

  • 1

    Exactly what Perozzo said I would answer now, avoid creating classes within the same file.

  • I get it. Classes are in the same project, so they belong to that project, so one has access to another, even if it’s private. But private-type properties belong to that class, so even if others are in the same project they don’t see them. Scope!

  • 2

    That’s right Heyjoe, for you not to let your class be instantiated even being in the same project, you need to leave the Construtor private.

  • Don’t confuse project with file. Project is the whole: classes, components, libraries, Solution itself. In his case, yes, the classes are in the same project, but in his case you can access them because they are in the same file. The correct thing to do is to create each class in its separate file.

  • 2

    Related: https://answall.com/a/238098/69359

  • Duplicate not missing! https://answall.com/questions/23/qual-%C3%A9-a-difference%C3%A7a-entre-modifiers-public-default-protected-e-private

  • @Perozzo when you speak file, you are referring to the class file: Person.Cs for example? If so, they are in different files, Pessoa.Cs and Parametro.Cs

  • @Heyjoe this! The correct is to separate the same files.

  • But it was already like this and it was being possible to instantiate it. Only when I tried to do the instance in another project it was not allowed to be private.

  • @Virgilionovic, could you point out where you talk about private classes in this question that you think is duplicate? Even more so in the context of C# which is different from Java. And why is this specific case answered there? In fact, you have been wrong enough to choose duplicates, and so far so good, because everyone has made mistakes, but it seems that you choose to close duplicates depending on who answered, and choose which duplicate or not to put as clear duplicate, depending on who answered in the original. This is not good for the site.

  • @Maniero maybe clicked wrong and I see no reason for so much and I also think exaggerate what you said about duplicates and stalking, but, that here on the site has https://answall.com/questions/237931/declares%C3%A7%C3%A3o-de-classes-in-c/238098#238098 and more https://en.stackoverflow.com/questions/156804/how-work-as-properties-no-c and this as complement https://answall.com/questions/23/qual%C3%A9-a-difference%C3%A7a-entre-modifiers-public-default-protected-e-private as duplicate of the question. If you think it’s not duplicate the two above!

  • @Maniero if you want besides I remove the duplicate I indicated that as I told you it was not intentional and yes choose in the list maybe by clicking in the wrong place...

  • @Heyjoe I was reading your question and noticed an information that does not match, if you put private in the class it is not accessible at all because the standard of the classes are internal if no modifier is placed. Your Person class should not be accessible because of the modifier .. the message is CS0122 'Pessoa' is inaccessible due to its protection level at least strange doesn’t even compile the application and you’re right to ask that.

  • @Virgilio Novic I made two classes Pessoa.Cs and Parametro.Cs; I put Pessoa.Cs as a private only for testing, if it was possible to access it, within the same project it is possible to access it by the Parametro class, for example. This error did not occur. My problem was not knowing that classes belong to that project as well as prop. belong to your class.

  • @Heyjoe has something that doesn’t add up, because I also took the test and it doesn’t work! It’s weird as I said.

  • @Heyjoe Did the answer resolve what was in doubt? Do you need something else to be improved? Do you think it is possible to accept it now?

Show 16 more comments

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 the type Pessoa is inside what? It should be deprived of what?

In the case may be visible only within another class or structure.

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

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

That is why I always say that you cannot learn by seeing the result, but 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 that way doesn’t mean it’s right. It’s an inglorious fight. Even people I’ve been through this kind of thing over and over again, still learning the wrong way.

  • Pessoa.Cs e Parametro.Cs did the test and I put Pessoa as private, even so in Parametro.Cs I can instantiate Pessoa. That was my question.

  • In fact, it was what I thought, but as I saw the comments, and I even thought it made a certain sense and has languages that are like that, I trusted my intuition and edited and put a reservation of the file (already removed), In fact the documentation is very explicit as to whether it only holds within a type, and specifically class and struct, since the other types do not make sense. Therefore all comments that speak of this in your question are wrong. Interestingly they have ups and this answer does not. Lesson: never trust your intuition, even if you are experienced.

  • Well, Richard Willian was right, within the same project I was able to instantiate her, into another project, not. But the others were wrong about the classes being of the same file, as they are not.

  • Your answer got a little complicated for me. rsrs As for your question "it should be deprived of what?" I only took a test, and I had noticed this again (that I could instantiate a private class) and found it strange, because following the logic of prop. private, I shouldn’t be able to access it.

  • Of course, the project no doubt, but not because it’s private, it’s because it’s internal. Yes, it is stated private, but it is obvious that this is less visible than internal then she’ll never be seen in places where internal cannot be seen. It can only be seen in other projects and is declared as public which is more than internal. internal is the standard visibility, and indicates the Assembly (or design, using the wrong terminology). Have you ever placed a property outside a class? If not, how can you use the same "logic"? My question remains valid.

  • I did not understand that class belongs to such a project as a prop. private belongs to its class. But now I understand.

  • In short, my question was "why can I access a private class by some other class being that as prop. private I can’t?" , I did not know how to formulate my doubt well. I simply did not know this question of class belonging to the project as well as the others. I’ll make a comparison, see if it’s correct: the project would be a class, the classes would be their properties.

  • My doubt remains, you thought it belonged to what then?

  • I thought I didn’t belong to anything, I just didn’t know about it, so the confusion. And my comparison is correct: the project would be a class, the classes would be their properties?

  • I thought the private would deny access to that class just as the private denies access to a property or a method.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.