2 ms·
The C# version was the same length 10 years ago. Now you would write: public Foo Foo { get; set; } and get the backing field for free.
by int_19h 10y ago
The C# version was the same length 10 years ago. Now you would write:
public Foo Foo { get; set; }
and get the backing field for free.
- pjmlp 10y agoNow write that with validation on set.
- breischl 10y agoSo your argument is that because there are some uses that require boilerplate, therefore all uses should require at least that much boilerplate?
- pjmlp 10y agoMy argument is that I use Java since 1998 and C# since 2002, and I don't see that as a big issue. There are bigger fishes to fry in Java than how properties are defined.
- kentosi 10y agoYou could do this in Java, but therein lies the cultural problem: From my experience, the Java world is so accustomed to get/set method doing nothing other than getting and setting the value that any attempt to do more (validation included) is seen as "surprising" and is frowned upon in code reviews. I'd typically have to create a special "setWIthValidation" method to please people... Bring in properties already.
- pjmlp 10y agoI have been using Java since 1998 and hardly seen it.
- int_19h 10y agoThat'll be about as long as Java, sure. But most properties that are actually written in code don't need validation. In fact, most of them don't even have a setter. In C#, this means that I can do: public Foo Foo { get; } or for a computed property: public int Foo => Bar + 1; The real savings from property come when you use them, though - the extra pair of () doesn't feel like much, but when you start chaining properties, I find that not having () there significantly improves readability. Then, of course, Java also insists on `get` prefix by convention. So you have Java: foo.getBar().getBaz().blah() vs C#: foo.Bar.Baz.Blah() More importantly, the latter visually emphasizes the difference between accessing data and operating on it. You can see that "Bar" and "Baz" are data members, conceptually similar to "foo", whereas "blah()" is an operation.