Classes & Objects
Constructors and fields work almost identically to java1-4. The real divergence is in how C# handles the getter/setter pattern every Java class relies on — it's not a pattern here at all.
Constructors — Quick Recap
Same idea, same shape as java1-4's own constructors — a method sharing the class's name, no return type, run once via new.
Properties — A First-Class Language Feature
In Java, "a property" is purely a naming convention — a private field plus a hand-written getName()/setName() pair, entirely a matter of programmer discipline that java1-4's own language rules never enforce or even recognize. C# has real property syntax built directly into the language: { get; set; } alone generates a hidden backing field and both accessors automatically — one line replaces what Java requires spelling out as three separate members.
Full Property Syntax with Custom Logic
A property can run genuine logic in its accessors — validation, transformation, computed values — while the call site still reads exactly like ordinary field access: customer.Name = " Alice "; silently runs the trim logic. This is a real, different call-site ergonomic from Java's explicit setName(...) method call, which always visibly announces itself as a method call.
Read-Only and Init-Only Properties
A get-only property can only be assigned inside the constructor. init (C# 9) is slightly more flexible — it allows object-initializer syntax to set the property exactly once, at construction time, then locks it — the same mechanism Course 2's own records chapter builds on internally for its immutable, boilerplate-free data carriers.
Access Modifiers, Revisited
C# has the same public/private/protected plus a genuine fourth level — but a structurally different one than java1-4's package-private. C#'s internal means visible anywhere within the same assembly (a compiled project/DLL), a unit defined by how the project is built, not by folder or namespace structure the way Java's package-private is tied to the package a file physically sits in.
| Aspect | Java (java1-4) | C# |
|---|---|---|
| Getter/setter pattern | a naming convention only | real language syntax — get/set |
| Call site for reading a value | customer.getName() — visibly a method call | customer.Name — looks like field access |
| The "fourth" access level | package-private (no modifier) — folder/package-scoped | internal — assembly-scoped |
| Immutable-after-construction field | final field, set once in constructor | init-only property, set once via initializer syntax |
{ get; set; } alone covers the overwhelming majority of cases — only expand to a full accessor body with explicit logic once a property genuinely needs validation, transformation, or a computed value.
Coding Challenges
Write a class Rectangle with auto-implemented Width and Height properties, and a read-only Area property computed via an expression-bodied get that multiplies them.
📄 View solutionWrite a class Customer with a Name property whose setter trims whitespace and rejects an empty string by throwing an exception, then demonstrate assigning " Alice " and reading back the trimmed result.
📄 View solutionWrite a class Product with an init-only Sku property, construct an instance using object-initializer syntax, then attempt to reassign Sku afterward and show the resulting compile error.
📄 View solutionChapter 4 Quick Reference
- Properties are real language syntax ({ get; set; }) — Java's getter/setter is purely a naming convention, per java1-4
- A property call site (customer.Name) looks like field access but can run real logic underneath, unlike Java's visibly-a-method-call getName()
- init (C# 9) allows object-initializer syntax to set a property exactly once, at construction — the mechanism Course 2's records build on
- internal is C#'s own fourth access level — assembly-scoped, structurally different from java1-4's folder/package-scoped package-private
- Next chapter: inheritance and polymorphism — virtual/override required explicitly, the reverse of java1-5's virtual-by-default