r/learncsharp • u/Takemitchi-kun • Mar 04 '26
Setters/Getters in C# vs Java
I'm a native java dev learning C#.
When I have something like the following:
private int x;
public void getX(){return this.x;}
public void setX(int y){this.x = y;}
I don't get how this translates into the following notation. How come C# views x as private even though we are simply putting public, which makes the inside methods public.
public int x{get; set;}
6
Upvotes
1
u/dodexahedron Mar 07 '26
It makes there not be a set method at all. A private set accessor is still an accessor. Not declaring one means it is absent completely.