c# - void method with arguments AND return value methods without arguments? -


i'm developing software in c# , remember read article following statement:

design methods of class this:

  • use void method with argument(s) in order change state of class instance. method changes on data of class
  • use return value method without arguments in order retrieve data class

i can't find article anymore , i'm wondering benefits , drawbacks of such convention in order improve quality , maintainability of code i'm writing.

my question is: know references/articles question? make sense follow these statements?

c# has properties think makes these suggestions less practical. big point take away data inside class should encapsulated (the outside world cannot access directly) , abstracted (the details hidden). 1 of primary purposes of class. reason think advice make more sense language c++ doesn't have properties.

one of problems though people write class private fields have write getter method , setter method each private field , results in redundant boiler plate code. properties streamline strategy while still maintaining encapsulation , abstraction. example,

userinfo user = new userinfo(); user.username = "foo"; console.writeline(user.password); 

in above example have set username foo , retreived password user. how set , retreived information hidden. may saved in .xml file right later decide change saving in database or directly in memory. outside world uses classes never wiser. 1 of many beauties of oop.

the 2 bullets in question can respond getter , setter of property. if want retrieve data class without arguments makes more sense have property. likewise if want change state of object can use setter property. can perform validation on input, make thread-safe, add logging, method takes single argument.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -