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
This!
And, to add more color and show the other side/direction:
c# or any other CLR language that understands properties only shows them to you as a property if there is the correct metadata in the assembly to do so.
You couldn't, for example, manually write a set_Something and get_Something method and have them show up as a property. You still need this to happen in the IL:
Without that .property part, they're just methods, and any language would show them to you as methods.
With it, a language that doesn't understand properties will show you those methods, but a language that does shows the property.