Encapsulation and Classes
Classes let you model the things your program works with, keeping their data and behavior together. You will define classes, create objects, write constructors, and protect data with properties.
Updated
Working with Objects in C#
- Working with Objects in C#: an overview (Novice): Object-oriented programming models software on the things around us, objects that each know some facts and do some jobs.
- What is an Object? (Novice): Every object has a responsibility; finding the objects in a problem, and the job of each, is the first step in designing a program.
- Defining an Object (Apprentice): An object's responsibilities split into what it knows (its state) and what it does (its behavior), and a class is the blueprint that describes both.
- Writing the Class in C# (Apprentice): Turn a class design into C# with private fields and public methods, then create objects with new and call their methods.
- What is a Constructor? (Apprentice): A constructor runs when an object is created and gives it the starting values it needs; use this to tell a field from a parameter of the same name.
Properties in C#
- Fields vs Properties (Apprentice): A property looks like a field from outside but runs code inside, so a class can let others read and set a value while it checks every change.
- Auto-Properties and Read-Only Properties (Apprentice): Write short auto-properties, make a property read-only to the outside with private set or get-only, and calculate a property from others.
- Methods on a Class (Apprentice): Write instance methods that use and change an object's state safely, keep its rules inside the class, and know when a method should be static.
Nullable Types in C#
- Nullable Value Types (Apprentice): Store "no value yet" in an int?, decimal? or bool?, check it with HasValue or null before you read it, and write an optional parameter that a call can leave out.
- The Null-Coalescing Operators ?? and ??= (Apprentice): Give a stand-in for a null value with ??, try several values in turn with a ?? b ?? c, and fill in a variable only when it's null with ??=.
- The Null-Conditional Operator ?. (Apprentice): Read a property, call a method or get an array item from something that may be null with ?. and ?[], which give null instead of stopping the program, and add ?? for a stand-in.
- Nullable Reference Types (Apprentice): Mark the strings, arrays and objects that may be null as string? or Player?, read the compiler's null warnings as crashes it sees coming, and answer each with a null check, a default, ?. or ??, keeping ! for the rare last resort.
Records in C#
- Records: Small Data Types with Value Equality and with (Apprentice): Write a small data type in one line with a positional record, which compares and prints by its values and makes changed copies with a with expression.
Exercise: Classes
- Exercise: Classes (Apprentice): Write six classes from a specification (Rectangle, Circle, MathTest, Printer, Elevator and Airplane), choosing each property's access, and let the tests check them.