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.
– HeyJoe
@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?
– Richard Willian
ah, so the private only serves if it is outside the project? I took the test here.
– HeyJoe
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.
– perozzo
Exactly what Perozzo said I would answer now, avoid creating classes within the same file.
– Thiago Loureiro
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!
– HeyJoe
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
.– Richard Willian
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.
– perozzo
Related: https://answall.com/a/238098/69359
– Rovann Linhalis
Duplicate not missing! https://answall.com/questions/23/qual-%C3%A9-a-difference%C3%A7a-entre-modifiers-public-default-protected-e-private
– novic
Possible duplicate of What is the difference between public, default, protected and private modifiers?
– novic
@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
@Heyjoe this! The correct is to separate the same files.
– perozzo
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.
– HeyJoe
@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
@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!
– novic
@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...
– novic
@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 isCS0122 'Pessoa' is inaccessible due to its protection level
at least strange doesn’t even compile the application and you’re right to ask that.– novic
@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
@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.
– novic
@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?
– Maniero