Reading — step 1 of 6
Learn
Classes and Properties
So far your data (a few ints and strings) 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 — so the rest of the program can say c.Area() without caring how area is computed. That bundling is the core move of object-oriented programming, and most C# you will ever read is organized this way.
class Circle {
public double Radius { get; set; } // auto-property
public double Area() {
return 3.14159 * Radius * Radius; // methods read the object's own data
}
}
public double Radius { get; set; }is an auto-property: the compiler generates a hidden storage field plus a getter and a setter. It reads like a field, but because it is a property you can later add validation ("radius cannot be negative") without changing a single caller. That future-proofing is why idiomatic C# exposes properties, never public fields.Area()is an ordinary method that uses the object's own properties — the data and the logic that operates on it live side by side.
Creating and using objects
A class is only a blueprint. new builds an actual object (an instance), and each instance carries its own property values:
var small = new Circle { Radius = 1 }; // object initializer syntax
var big = new Circle { Radius = 10 };
Console.WriteLine(small.Area()); // 3.14159
Console.WriteLine(big.Area()); // 314.159
{ Radius = 1 } is object initializer syntax: construct the object and set its properties in one expression. The long form is var c = new Circle(); c.Radius = 1; — identical result.
Classes can also declare constructors when data must be supplied at creation time:
class Circle {
public double Radius { get; } // get-only: set once, at construction
public Circle(double r) { Radius = r; }
}
var c = new Circle(2.5);
Either style works for this lesson's exercise; the object-initializer style needs the least code.
The trap: no object, no call
// Trap 1: calling an instance method on the class itself
Console.WriteLine(Circle.Area()); // error — whose radius? Area needs an instance
// Trap 2: forgetting public — members are private by default
class Circle {
double Radius { get; set; } // private! Main cannot see it
}
Area() reads Radius — whose radius? A specific object's. You must new up an instance and set its properties before you can call methods on it. And in C#, class members default to private: forget public on a property or method and Main cannot even mention it. (Methods that genuinely need no instance are marked static — that is exactly what Main is.)
Your exercise
Define a Rectangle class with two int properties, Width and Height, and an Area() method that returns Width * Height. In Main — which already reads w and h for you — construct a Rectangle, set both properties (object initializer syntax is perfect here), and print the result of Area().
The grader expects the bare number: for inputs 3 and 4, exactly 12. Printing Area: 12 or any extra words fails the character-for-character comparison. The hidden test passes 0 and 5 and expects 0 — which works automatically if Area() truly multiplies; do not special-case zero.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…