class - Why use getters and setters in c# -


this question has answer here:

i have used 2 ways setting public class in c# (and vb.net)

option 1 - setters & getters

public class foo {   private string _bar;   public string bar   {     { return _bar; }     set { _bar = value; }   } } 

option 2 - simple public class

public class foo     public string bar = "";    end class 

i've done both ways, it's simpler , less code option 2 - , don't see difference in terms of other coding set or values class or performance - both ways work fine.

my question "why option 1 if there not other 'special things' happening in getter or setter? , if - reasons potentially be?

what want know if there's 'convincing argument' using setters , getters , if 'specifically why?

cheers, charlez

for specific situation, faster have public variable (all getters , setters turned functions @ compile time).

unlike java, if have getter/setter functions must called functions. if @ point want change public variable getter/setter code base doesn't have change (but must still recompiled) means if developing library, , want hot swap dll , change public variable getter/setter break target program.


Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

php - HTTP_REFERER woes: How can I allow access to a specific page, only when a visitor has visited another specific page beforehand? -

java Extracting Zip file -