10. Classes and Objects#
An invoice has a number, a customer, line items, and rules: a total cannot be negative, and a number is never issued twice. Without a class, that data and those rules scatter across parallel arrays and methods that every caller must remember to use correctly. This chapter shows you how to bundle them into a class. You will declare fields, constructors, and methods (10.1), protect an object’s state with properties and ToString (10.2), and trace how objects change, share references, and live in lists (10.3). A supporting reading on tuples and parameter arrays (10.4) adds two techniques for returning and accepting several values.
Class and object: A class defines the fields, constructors, properties, and methods for a kind of thing; an object is one instance of it with its own state.
Encapsulation: Private fields plus controlled public members keep an object valid.
Property: A property reads or writes state through a
getorset(orinit) accessor that can enforce a rule.Reference: A class variable refers to an object, so two variables can alias one object;
nullmeans no object.Static member: A static field or method belongs to the class and is shared by every object.
Chapter Video
C# classes 📦 (4:53) by Bro Code.
Hosted on YouTube; property of its original creator, embedded here for educational reference only.
Learning Objectives
By the end of this chapter, you will be able to:
Declare a class with fields, constructors, and instance methods for a stated business responsibility, and create objects that keep independent state.
Write overloaded constructors that share one validation path through
this(...)delegation, and explain howthisdistinguishes a field from a parameter.Distinguish instance members from static members, and use a static field,
const, orreadonlyfor a fact that belongs to the whole class.Protect object state with private backing fields and properties, choosing among full, auto, get-only,
private set,init, and computed properties, and keep related values valid together.Use object initializers for optional values while keeping required starting state in the constructor, and override
ToStringso it returns a description.Trace operation sequences on independent objects, and explain aliasing,
nullreferences, and why a copy constructor is needed to get an independent copy.Pass objects to methods and store them in a
List<T>, searching and totaling by property, and choose the internal state that supports a class’s public contract.Check constructor, property, and operation contracts with normal and invalid inputs.
Supporting-reading outcome: return and deconstruct tuples, and write a method that accepts a variable number of arguments with params.
Chapter Flow
10.4. Tuples and Parameter Arrays (Supporting Reading). Read the
paramssubsection before Homework Question 19.
Suggested three-meeting pace (about 115 minutes): read the relevant explanations and predict the selected example before class.
Meeting |
Minutes |
Guided focus |
|---|---|---|
1 |
35-40 |
10.1: classes and objects, constructors and |
2 |
35-40 |
10.2: why public fields are risky, properties and validation, access control, object initializers, and |
3 |
35-40 |
10.3: state changes in a register, aliasing and |
Class uses selected traces, runs, and checks. Use the remaining worked examples for practice and reference. Section 10.4 is a supporting reading and adds no meeting; lab and homework follow the core lessons.
Glossary
# |
Term |
Explanation |
|---|---|---|
1 |
Class |
A programmer-defined type that groups data and behavior. |
2 |
Object |
An instance of a class created while a program runs; it has its own state. |
3 |
Field |
A variable declared in a class that holds state, per object or, if static, for the whole class. |
4 |
Method |
A named behavior defined by a class. |
5 |
Constructor |
A special method that initializes a new object into a valid starting state. |
6 |
Constructor overload |
Another constructor in the same class with a different parameter list. |
7 |
Constructor delegation |
A constructor using |
8 |
this |
The keyword that refers to the current object inside an instance member or constructor. |
9 |
Access modifier |
A keyword such as |
10 |
Encapsulation |
Keeping state private and exposing controlled operations so an object stays valid. |
11 |
Invariant |
A rule an object preserves whenever construction or a public operation completes. |
12 |
Property |
A member whose accessors read or write a value under the type’s rules. |
13 |
Accessor |
A property’s |
14 |
Backing field |
The private field that a property’s accessors read or change. |
15 |
Auto-property |
A property whose compiler-generated backing field needs no explicit code. |
16 |
Init accessor |
An accessor that allows assignment only during construction or in an object initializer. |
17 |
Object initializer |
Brace syntax that assigns accessible members right after the constructor runs. |
18 |
ToString |
A method that returns a text description of an object; |
19 |
Static member |
A member that belongs to the class itself rather than one instance. |
20 |
Static field |
A field with one copy shared by every object of the class. |
21 |
Instance member |
A member that belongs to a particular object. |
22 |
Reference type |
A type whose variables refer to objects instead of storing all object data. |
23 |
Alias |
Another reference to the same object; assigning a class reference does not create a second object. |
24 |
Null reference |
A reference that refers to no object; using it throws |
25 |
Copy constructor |
A constructor that builds a new, independent object from the state of an existing one. |
26 |
Tuple |
A value that groups a few components, optionally with names (supporting reading). |
27 |
Deconstruction |
Assigning a tuple’s components to separate variables by position. |
28 |
Parameter array |
A final |