C
C#14mo ago
Asriela

❔ Hi for some reason this exercise on indexing isnt clicking for me can someone guide me this?

1-im used to properties that have a name this property has no name so i guess it refers to the class but then it accesses the name field... But why... 2 i guess we access the name field by just referring to the ovject "c" , y not just go c.name[0] whats with this weird ass obfuscation
No description
No description
No description
6 Replies
Asriela
AsrielaOP14mo ago
Maybe i just have an issue with the example they gave because it makes the code less readable. Since it hides the variable name you are maccessing/modifying. Thus c.name[0] is much better for general readability. So i guess what im asking is there maybe something that im missing or did they just give a terrible example? If so does anyone have a good example for me, maybe? Thank you!
Sossenbinder
Sossenbinder14mo ago
It's mostly syntactic sugar, it mostly works for collections or lookup types Take a dictionary for example, it does support accessing values by key through an indexer I'd argue it's a bit of a niche feature for sure though From an encapsulation point of view one could make the point that an indexer would allow index based access of data without exposing a specific backing storage So in your example you might expose an array to the outside which can be indexed, but what if the internal data structure changes from an array to another structure which does allow index-based access, but would require everyone consuming this to change how they access the property, since it's not an array anymore internally It's really syntactic convenience akin to how we use arrays or Dictionaries naturally, after all there's nothing keeping you from implementing this behavior in another way
Asriela
AsrielaOP14mo ago
Sweet! Thank you this clears it up!
sibber
sibber14mo ago
$close if you have no further questions :)
MODiX
MODiX14mo ago
Use the /close command to mark a forum thread as answered
Accord
Accord13mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server