Private class is very little useful because it is available only within another class, that is, only the external class can instantiate an object of the inner class that is private. Not even returning an object of this type outside the class is possible because the type does not exist outside the class.
When the type is really only useful internally there is no reason to let the class be accessible from outside. The smaller the scope, the less errors you may have in your code, the less pollution namespace will have, so it is easier to locate what you want.
There is little reason, but there are cases for its use. This is done to hide implementation details. So one day you can change whatever you want without worrying. Everything that is exposed out of class becomes a contract that you have to respect under penalty of breaking existing codes.
A simplified and unnecessary example in this context, just to illustrate:
public class Classe {
Temp temp = new Temp();
public string Texto { get => temp.Str; set => temp.Str = value; }
public int Valor { get => temp.I; set => temp.I = value; }
private class Temp {
public string Str { get; set; }
public int I { get; set; }
}
}
I put in the Github for future reference.
You cannot create an object of the type Temp
out of this class.
One thing few people know is that the standard of the class is internal
, that is, it is only accessible within the file itself Assembly code. To make the class accessible to every application you need to use the attribute public
in it.
In other languages an internal class can have other functions, and if you try to learn from them, you won’t be learning how it works in C# which is just that.
I feel like the answers to that will be based primarily on opinion, like your naturalist professor, but I’m going to participate =)
– Diego Rafael Souza
@Diegorafaelsouza? Hope to know some development patterns that I have not seen yet and that can use this way... Anyway... Let’s go :)
– DiegoSantos
Possible duplicate of What is the difference between public, default, protected and private modifiers?
– Rovann Linhalis
The question should not focus on other languages, only on C# since they have already positioned as duplicate in C# has some differences
– novic
So @Rovannlinhalis, there is treated the access modifier. Here I’m asking more about the same about a class. What’s different, no?
– DiegoSantos
Yeah. In fact. It’s marked as
java
, but it really all applies to the general concept of OO. Specifically this answer I think it says what you want to know.– Diego Rafael Souza
Good @Virgilionovic, edited. Thanks!
– DiegoSantos
@Diegorafaelsouza, actually, from what I understand, they talk about attributes and methods. I believe that classes have different treatments, don’t you? After all, how to access it? Not a class in the same namespace could use it... I mean, it would be useless?
– DiegoSantos
As @Diegorafaelsouza said, despite being in java, the concept of OO is the same... if your teacher only makes small projects, should not even make a difference to him... But basically you use the
private
whenever that resource (class, variable, method, etc.) is only available in the class itself. There are some security factors to work like this, (including to protect the program of the own programmer rsrs) besides being very useful for organizing the codes / projects within a very large Solution– Rovann Linhalis
Really, the concept of OO is the same. But you see, creating a private class is different than creating a method or a private attribute. If you create a private class, who will use it and how? That I don’t understand...
– DiegoSantos
you can create a class within another... within a control, within a Form... in which case it would be restricted to the Higher class. Here is a simple example: https://dotnetfiddle.net/9jcmrl
– Rovann Linhalis
@Rovannlinhalis, now things are starting to make more sense. Are there other examples, perhaps patterns? If you can elaborate an answer to that I thank you
– DiegoSantos
@Rovannlinhalis, I confess that I found very strange this concept, but it really works...
– DiegoSantos
Once I get home, if not an answer that clarifies your doubt, I’ll try to do it. But it is quite possible that soon there will appear here a reply from Mario explaining everything perfectly rsrs
– Rovann Linhalis
Thanks Dude. huiahauhau Let’s see :)
– DiegoSantos