Skip to content
Lesson 7 of 12

Step 1 of 6 · Reading · ~3 min

Learn

Classes and Objects

Classes and Properties

By the end of this lesson you can bundle data and behavior into a class, create objects from it, and explain what happens when two variables refer to the same object.

So far your data (a few variables) and your logic (the code in Main) have floated around loose. A class bundles them into one named thing: data as properties, behavior as methods. The rest of the program can then say ticket.IsExpired() without caring how that is computed.

class Circle {
    public double Radius { get; set; }       // auto-property

    public Circle(double radius) {           // constructor: same name as the class, no return type
        Radius = radius;
    }

    public double Diameter() {
        return Radius * 2;                   // methods read the object's own data
    }
}
  • public double Radius { get; set; } is an auto-property: the compiler generates a hidden field plus a getter and a setter. It reads like a field, but because it is a property you can later add checks (such as "the radius cannot be negative") without changing any caller.
  • The constructor runs when you create an object and puts it into a valid starting state. Radius = radius; assigns the parameter to the property.
  • Diameter() is an ordinary method; it uses the object's own Radius.

Objects and instances

A class is only a blueprint. new builds an actual object (an instance), and each instance carries its own property values:

Circle small = new Circle(1.5);
Circle big = new Circle(10);
big.Radius = 12;                         // changes only the big one
Console.WriteLine(small.Diameter());     // 3
Console.WriteLine(big.Diameter());       // 24

If a class has settable properties you can also use an object initializer, which constructs the object and sets properties in one expression: new Circle(1.5) { Radius = 2 }. A this. prefix (this.Radius) names the current object explicitly; you need it when a parameter has the same name as a property.

Printing a bool shows True or False with a capital letter, a C# quirk worth remembering when a spec asks for exact output.

Classes are reference types

A variable of a class type does not hold the object; it holds a reference to it. Copying the variable copies the reference, not the object:

Circle a = new Circle(2);
Circle b = a;          // b refers to the SAME object
b.Radius = 9;
Console.WriteLine(a.Radius);   // 9 -- the change is visible through a

Compare with int, where int y = x; copies the value. A method that receives a class instance can therefore change the caller's object.

Common mistakes

  • Forgetting public. Members are private by default, and Main cannot even mention a private property.
  • Calling an instance method on the class. Circle.Diameter() fails: whose radius? Create an object first. (Methods that need no object are marked static, like Main.)
  • Expecting b = a to copy an object. Both names refer to one object.
  • A method that computes a new value but never stores it. Writing Radius * 2; as a statement does nothing; assign the result back to the property.

Your exercise

You write the whole Rectangle class and the Main code that uses it: the starter only reads the input. Follow the member names in the exercise exactly, because the output depends on them. The most likely mistake is a method that is meant to change the rectangle in place but only computes a value without storing it.

Up nextLists and DictionariesCollections and LINQ

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…