Classes & Objects

Course 1 · Ch 4
Classes & Objects
Properties are a real language feature here, not a naming convention java1-4 left to discipline

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

class Account { private string _owner; public Account(string owner) { _owner = owner; } // same shape as java1-4 }

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

public class Customer { public string Name { get; set; } // auto-implemented property — one line } Customer c = new Customer(); c.Name = "Alice"; // looks like direct field access... Console.WriteLine(c.Name); // ...but is really calling get/set underneath

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

private string _name; public string Name { get => _name; set => _name = value.Trim(); // real logic, still called like plain field access }

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

public string Id { get; } // read-only — settable only inside the constructor public string Sku { get; init; } // init-only (C# 9) — settable once, at construction var product = new Product { Sku = "A1" }; // object-initializer syntax works with init

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.

AspectJava (java1-4)C#
Getter/setter patterna naming convention onlyreal language syntax — get/set
Call site for reading a valuecustomer.getName() — visibly a method callcustomer.Name — looks like field access
The "fourth" access levelpackage-private (no modifier) — folder/package-scopedinternal — assembly-scoped
Immutable-after-construction fieldfinal field, set once in constructorinit-only property, set once via initializer syntax
Default to auto-implemented properties
{ 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.
A property that looks like a field can hide real, possibly expensive logic
Because a property's call site is indistinguishable from plain field access, a getter or setter with genuine work inside it — validation, a database call, anything non-trivial — is invisible at the call site in a way a Java method call never is. Keep property accessors cheap and side-effect-free as a matter of discipline, since nothing in the syntax itself signals otherwise.

Coding Challenges

Challenge 1

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 solution
Challenge 2

Write 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 solution
Challenge 3

Write 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 solution

Chapter 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