Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# getters and setters method definition - beginner

Tags:

c#

I am new to C#. i was going through a tutorial. and it shows how to create accesor-mutator to a variable as shown below;

public String var1 {

get {return "";}
set {someVar = value;}

} 

1.) Can't i create getters and setter like created in java

public getVar() {return "";}
public setVar(String x){var=x;}

2.) What is value used in C# ?

like image 447
Sharon Watinsan Avatar asked Dec 03 '22 00:12

Sharon Watinsan


1 Answers

  1. You can, but that's much more annoying to use, and ignores C# coding guidelines.

  2. value is the implicit parameter to the setter. It contains the value that the caller is setting the property to. (the right side of the Property = something call)

See the documentation.

like image 106
SLaks Avatar answered Dec 17 '22 07:12

SLaks