Getter and Setter in C# -


i'm playing around c# , i'm aksing myself proper method getter , setter. found google:

class myclass {     button btnmybutton;      // code...      public button getbtnmybutton     {                 {             return btnmybutton;         }     } } 

is there 'proper' way? or okay:

class myclass {     button btnmybutton;      // code...      public button getbtnmybutton()     {          return this.btnmybutton;     } } 

whats difference?

as thomas mentioned, same things. may wonder point of getter , setter in case , it's syntactic sugar. however, means don't have create explicit field 1 created in background. therefore, can do

public button mybutton { get; private set; } 

the private set; ensures class can set value it's read-only outside classes. removing private allow external classes write variable too.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -