I have raised this issue a few times on the site chat but I feel there is a lot of diversity of opinion and it seemed appropriate to post a question on the site.
Starting from the base of the four pillars of OOP
- Abstraction
- encapsulation
- Inheritance
- Polymorphism
I believe that what is called OOP in C# does not have attribute encapsulation.
Encapsulating an attribute, from what I've studied, means leaving the attributes in question as private
.
This leads me to think that something that is not is usually called object-oriented programming.
From what I've always seen, to define a class attribute in c# is done as
public int number { get; set; };
By doing this you are ignoring one of the fundamental pillars of OOP, which is variable encapsulation.
Wouldn't it be more correct to call it property-oriented programming, or something like that? Since the attributes are not privatized and they are accessible from any class, even if they are modifiable or not.
No, encapsulation means having control over how an object looks to the user of the object. This is achieved by separating data that " needs to be known " externally from data that "needs to be hidden ". Wikipedia explains it pretty well (emphasis mine):
Therefore, posting internal data of an object does not break the encapsulation, what breaks the encapsulation is doing it uncontrollably or posting internal data that the user does not need to know.
It may be easier to understand with an analogy :
I think there is some kind of confusion in this.
It is possible that you have seen C# code generated in Visual Studio where
public int number { get; set; };
it is autogenerated by using the prop + TAB snippet that generates the code that you have put inget
public andset
privateIf you use the propfull + TAB snippet TAB generates this.
So I think that you have simply seen autogenerated code and that normally it is still used
private int miVariable
. I don't know if this is what you were asking. All the bestEDIT:
Using this wrapper at the end
public int number { get; set; };
Visual Studio internally creates a private int number