← All lessons

Level 1 / C# Fundamentals / fundamental 7 of 7

Classes and objects

Define classes with properties, constructors, and methods, then create and use objects.

A class bundles data and the methods that work on it into your own type. This is where every earlier drill comes together: properties are variables, methods use if and loops, and a list can live inside an object. It is also the foundation for the Razor and MVVM tracks, which are built entirely out of classes like these.

Understand the idea

A class is a blueprint

class Truck { ... } describes what every truck has and can do. new Truck() builds one actual truck, an object, and you can build as many as you need, each with its own values.

Properties hold an object's data

public string Make { get; set; } is a property: a value each object carries that other code can read (get) and change (set). Access it with a dot, as in myTruck.Make.

Constructors and methods

A constructor has the class's name and no return type, and runs when the object is created so it starts out valid. Methods inside the class can read and change that object's own properties by name.

English to C#

“Write a class called Truck with a public string property called Make.”

class Truck
{
public string Make { get; set; }
}
class Truck
a new type named Truck
public string Make
a property everyone can see, holding a string
{ get; set; }
it can be read and changed

Rep 1 of 12

Copy it

In English

Write a class called Truck with a public string property called Make.

Type the code shown below. Watch it turn green as you go.

class Truck
{
public string Make { get; set; }
}

Waiting for you to type · 0 of 13 pieces

Ctrl+Enter checks or moves on. Tab indents. Paste is off on purpose: your hands are learning this too.

Reps

0 of 12 done

Reps unlock in order, because each one builds on the last.

How your code is checked

Drills compare what you type with known-correct C#, token by token. Spacing, line breaks, and comments do not matter; spelling, case, and punctuation do. It is not a compiler, so it accepts the listed spellings of each answer and nothing else.

Try it in a real .NET project

Model something you know: a Truck with Make, Mileage, and a Drive(int miles) method, or a Crew with a list of members. Create three objects in Program.cs, call their methods, and print their properties. Then open the Razor track: a page model is exactly this kind of class.

Check your understanding

If you create two Counter objects and call Increment on the first one twice, what is each Count, and why?

Show the explanation

The first is 2 and the second is 0. Each object built with new gets its own copy of every property, so calling a method on one object only changes that object's data. The class is the shared blueprint; the objects hold separate values.

Goose School teaches Microsoft's stack. Goose Solutions is not affiliated with, endorsed by, or sponsored by Microsoft.