1

I understand the need for encapsulation and the variables in a class declared as

 public String VariableName{get; set; } or 

 private string _variableName;
 public string VariableName {
    get {
       return _variableName;
    } 
    set {
       _variableName=value;
    }
}

One of the reasons for doing this I understood is to avoid threads accessing the variable during race conditions and changing the class property. But this could not only be the reason of this encapsulation. How can other classes change a variable of this class? I think to directly access a property we would need to use the static keyword. Otherwise, the property can only be changed by creating objects of this class. Also, how does this actually encapsulate? We can access the _variableName field using the VariableName property of the object. So, essentially we still have access to it. Could someone please help me understand this concept?

TIA.

Zankhana Rana
  • 169
  • 2
  • 16

2 Answers2

0

So when you create your value as private, it only can be accessed by the class that you created. Even if you create an instance of that object, you cannot directly call the private variable inside of the other class. That is why we create get and set methods. This lets us change what we want to change about that object whenever we call it. So if it's called in somewhere, that must be on purpose not by accident. Actually this is what I know.

Edit : Additionally, when you use FINAL for your variable, it will never ever be changed by anything in any condition. So this is why we also use FINAL.

Mystheman
  • 87
  • 7
  • For instance, I code in Java and when I use int final x = 5; the value of x always will be 5. Even if you create a set method it will not be able to change it. This might be the idea that you wanted. So it's once created and cannot be changed. – Mystheman May 11 '20 at 06:56
  • In C#, that is `readonly` and it is language concept only. The field can be changed by reflection. – Paulo Morgado May 11 '20 at 07:03
0

There are quite a few misconceptions in your question. I recommend you to check the C# documentation.

First of all, using properties has nothing to do with threading. Properties are about the class author controlling the access to the data held by the class. Some of that control might be related to threading.

Interfaces are allowed only to declare properties, not fields. So, interface implementations must use properties.

The static keyword is used to declare members that belong to the type itself rather than to a specific object.

As to your example:

public String VariableName{get; set; }

This nothing more than compiler "syntactic sugar". The compiler translates that code to:

[CompilerGenerated]
private string <VariableName>k__BackingField;

public string VariableName
{
    [CompilerGenerated]
    get
    {
        return <VariableName>k__BackingField;
    }
    [CompilerGenerated]
    set
    {
        <VariableName>k__BackingField = value;
    }
}

To avoid name collisions, the property's backing field has a name that is valid in IL but not in C#.

Paulo Morgado
  • 14,111
  • 3
  • 31
  • 59