Classes and instances
A class defines a type with data and behavior. An object is an instance of a class, created with new. Two separate instances can store different values even though they follow the same definition. A variable of a class type holds a reference to an object, not a copy of the object itself.
using System;Bin left = new Bin();Bin right = new Bin();left.Quantity = 4;right.Quantity = 9;Console.WriteLine(left.Quantity);Console.WriteLine(right.Quantity);class Bin{ public int Quantity;}Each new expression creates a separate object with its own integer field.
Expected output (reasoned, not executed):
49Create and use instances
A class declaration names the type and contains its members. An instance field stores data for each object; an instance method works with the object on which it is called. In this example public fields make the model visible for teaching only. The next lessons replace them with controlled state. Top-level console statements must precede class declarations in these single-file examples.
Identity and shared references
Assigning a class reference to another variable does not create another instance. Both variables can reach the same object. object.ReferenceEquals(a, b) checks whether two references designate the same object (or both are null); it does not compare the values of fields. Two objects can have equal-looking data without being the same object. A class variable can also hold null; nullable annotations help detect mistakes but are not a runtime guarantee.
using System;Bin bin = new Bin();bin.Quantity = 5;Console.WriteLine(bin.HasStock());Bin empty = new Bin();Console.WriteLine(empty.HasStock());class Bin{ public int Quantity; public bool HasStock() => Quantity > 0;}HasStock reads the field of the receiving object. The second bin starts with the default integer value zero.
Expected output (reasoned, not executed):
TrueFalseusing System;Bin original = new Bin();original.Quantity = 2;Bin alias = original;Bin separate = new Bin();separate.Quantity = 2;alias.Quantity = 8;Console.WriteLine(original.Quantity);Console.WriteLine(separate.Quantity);Console.WriteLine(object.ReferenceEquals(original, alias));Console.WriteLine(object.ReferenceEquals(original, separate));class Bin { public int Quantity; }The alias reaches the original object; separate is independent even if its initial field value matched.
Expected output (reasoned, not executed):
82TrueFalseCommon mistake and correction
Bin copy = original; is not an object copy. To create an independent bin, construct another instance and explicitly initialize its state. Copying nested mutable references requires an additional policy; do not assume shallow copying duplicates every reachable object.
Try it yourself
Create a Counter class with an integer field and an instance method Increment() for this introductory model. Create two counters, increment the first twice and the second once. Assign an alias to the first and increment through it. Print both final counts.
Next: Constructors and invariants. Examples target C# 14 / net10.0. They are static study material; compiler and ByteMind execution have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Classes and instances — knowledge check
Apply the lesson concepts to the following scenarios.
What creates an independent Bin object?
Which quantity does bin.HasStock() read?
What is the initial integer field value when no initializer assigns it?
What happens after Bin b = a; b.Quantity = 7?
Two independently created bins have Quantity 2. What does ReferenceEquals return?