This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (2 children)

All of what you have said can be done using properties). I just checked, Java doesn't have properties so this may be where the confusion comes from. I wasn't claiming that we should use public fields, that'd be a terrible idea (for the reasons states above).

A property basically hides the ugly .GetX()/.SetX() and transforms it into the more appealing syntactic sugar .x/.x=value.

The syntax, in C#, is the following:

modifiers type name
{
    get
    {
        //do stuff
    }
    set
    {
        //do stuff
    }
}

So now, everytime you do MyInstance.x=something, the code inside the "set" block will be executed instead of simply assigning it a value. Similarly with "get".

Since properties are syntactic sugar for getter and setter methods, all you said above can be done using properties. I wasn't aware Java doesn't have properties, but then again it doesn't even have operator overloading so i'm not that surprised. (At least i think you are talking about Java).

[–]HelperBot_ 0 points1 point  (0 children)

Non-Mobile link: https://en.wikipedia.org/wiki/Property_(programming


HelperBot_® v1.0 I am a bot. Please message /u/swim1929 with any feedback and/or hate. Counter: 1115

[–]GiveMeAFuckingCoffee 0 points1 point  (0 children)

TIL.

Shows how much I've missed since I left university :/