#properties in c#
1 messages · Page 1 of 1 (latest)
because you can just modify a public variable. the language gives you that feature by default
yeah the argument their making is that its not tidy and can lead to unintended uses of that public field
as a convenience. it also is more legible when doing certain things like vector math, to not be calling .getX() every time, instead of just .x
you can modify a property as well with the same syntax
live by the sword, die by the sword
yeah my issue with "encapsulate everything with a property" is that its simply just more verbose. more lines for the same functionality
same amount of lines
we don’t measure in # of lines, but legibility
if youre saying i should do public int thing {get; private set;}
link to what you mean by legibility
public float f;
public float f1 { get; set; }
This is not achievable with just field btw
some things should have an extra layer of protection, because you really don’t want anything to fuck with it.
and that is ok
not everything needs that by default
but having it by default is a good practice, consistent, explicit
Just to absolutely clear, C# properties aren't like getters explicitly written out as methods... No one's arguing for creating GetX() methods
thats if i need something to only be read, and not written. but if i need something read and written, (which is VERY often the case) then the field is faster
You don't want to make bunch of diffs by changing field into property
Faster.. for what?
i thought that is what we are arguing about
Faster to write the code?
Set up your IDE snippets sir
look im not arguing against properties when theres encapsulation necessary. but im just defending the idea that public fields are okay in many circumstances too.
if i weren't part of a team that already had standards in place against that
I’m also not arguing to make everything needlessly public. But if you want something to be public… just make it public.
and use it
Honestly I think this argument only exists because Unity's serialization rule, they got comfortable with using public field
https://learn.microsoft.com/en-us/archive/msdn-magazine/2018/june/csharp-tuple-trouble-why-csharp-tuples-get-to-break-the-guidelines
This is a good article with examples show why we shouldn't use public field (and explains why ValueTuple uses public field, which does not apply most of the other cases)
I have this looming feeling of doom. Where discord says someone has been typing for a while, and you know a lot of keys have been pressed.
Properties are members which are accessed using the same syntax as fields (e.g. .x for get .x = whatever for set), but those operations can be individually implemented or have different levels of access, etc. It's a field-like interface with a lot more flexibility and control - the ability to do things with it which you might think to use a method for, without needing to create a method, or use method syntax
you mean to assign different levels of permission to read and write?
Sure that's one use-case. Like this is an "automatic property" for which C# will create a hidden backing field, with different levels of access per operation:
public float x { get; private set; }
Executing validation logic on an assignment is another common use-case - which, if you were just using fields and methods - would force you to switch to a method
can we agree that
public int x { get; set; }
is functionally identical to
public int x;
just with more text on the screen?