Availability of functions and variables - C#
Hello people!
I want to share with you awsome knowledge! One of the bases of objectivity is encapsulation. That mean - something can be touch from outside of class, different things don't.
We call it encapsulation or hermetisation of object and it is awsome! For example you got a secret and no one know it - you can hide it in a object and call it private - that's mine!
So in C# we have 3 types of information:
- public - everyone can call it and change it (if it is variable),
- protected - mean that just you and object which got you inside them can handle you,
- private - no one just object go information and can use it, even objects which got you have no access to data.
In C# you need to write explicit which function is not private (private is default state) int counter; - private
private string counterName;
public int GetCounter() { return counter; }
protected void SetCounter(int newCounter) { counter = newCounter; }
Good practices in hermetisation is to make getters and setters for variables, variables should always be not accessible (private or protected). Functions (even constructors can be private), but the most important thing you need to know - think about what you do!