encapsulation, accessor C# vs Java -


a quick question encapsulation , accessor in c# vs java. code in c# equivalent 1 in java below?

c#:

class myclass{      public string var1 {get; private set;}     public int var2 {get; private set;}      public myclass(string astring, int anint){         this.var1= astring;         this.var2=anint;     }  } 

java

//java class myclass{      private string var1;     private int var2;      public myclass(string astring, int anint){         this.var1= astring;         this.var2=anint;     }      public string getvar1(){         return this.var1;     }      public void setvar1(int anint){          this.var1 = anint;     }      public int getvar2(){         return this.var2;     }      public void setvar2(string astring){          this.var2 = astring;     }  } 

i'm coming java world, not sure accessor shortening in c#.

no, in c# code you've got private setters - in java code they're public.

the c# equivalent java code be:

public string var1 { get; set; } public int var2 { get; set; } 

(i've changed names follow .net naming conventions - properties pascalcased.)


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -