which is the proper way to code getters and setter on c# | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

which is the proper way to code getters and setter on c#

public int getX(){ return x; } Or public int X {set => x = value; get => return x;}

6th Feb 2021, 5:21 AM
Diego Becerril
Diego Becerril - avatar
3 Answers
+ 2
it's a matter of choice it doesn't really matter. the second example is fully introduced in C# 7 ( it was limited in C# 6) form Microsoft docs. " Expression body definitions let you provide a member's implementation in a very concise, readable form." so it's about readability, I guess.
6th Feb 2021, 8:46 AM
Bahhaⵣ
Bahhaⵣ - avatar
+ 1
The second one. The first one is just a method it does not make X a property.
6th Feb 2021, 11:53 AM
sneeze
sneeze - avatar
+ 1
public string Title{get;set;}
6th Feb 2021, 2:04 PM
Anthony Tannous
Anthony Tannous - avatar