Understanding Properties

 Now a days properties has become rule to declare in every class. I don’t think it should be used in every  class that we declare. It all depends on what is the scenario in our project whether we need properties or declare them? Well I guess it should be used in all classes because as we all know that variables inside class are private by default and cannot be accessed outside it’s class. In order to access private variables of the class then we have only two options

Declare with public - not good idea as anything can be set into it and by anyone.

Declare with properties

Advantage by declaring properties is that, either we can allow only to read access with “GET” or we can give SET access by checking some conditions and then only set those values.

I think if a property is declared with get and set without special conditions inside get and set then it is better to declare them with public


we can also declare property which is of type ENUM. we can also define property which is of type EVENT

in auto initializer property if it is only returning any value which will never change then we can directly assign like below

Public string Country{ get;} = "India";

i cannot use this auto initializer property if there are any conditions inside set. It can only be used if there are no conditions inside the property


Comments

Popular posts from this blog

Understanding Collection and Object Initializer

How to execute Javascript in VS code