I am trying to understand encapsulation and reading a lot of about it. Generally, it is told that properties(c#), getters/setters(java) are evil. I can understand that because consumers can use exposed data in an unexpected way.
But at the same time, I have problems on this perspective. For example, I have this class:
public class Ad
{
private readonly long _groupId;
private readonly string _path;
private readonly bool _paused;
private readonly string _label;
public Ad(long groupId, string path, bool paused, string label)
{
_groupId = groupId;
_path = path;
_paused = paused;
_label = label;
}
//some methods here
}
//some other code in another class
//... add Ad objects to List<Ad> adList
adList.GroupBy(x => x.??) //cannot groupId
I don't have any properties or getters/setters here. But then I am creating a list of this objects and trying to group them by _groupId or sort them by _label. I cannot do that because I don't have access to this properties.
Could you please enlighten my way on understanding encapsulation?
Thanks in advance.