What a class is
So far you have used types that C# provides: int, string, arrays. A class lets you create a type of your own. It is a blueprint that groups related data and behaviour under one name. A Dog class could describe what every dog has (a name, an age) and what every dog can do (bark). A BankAccount or a Game works the same way.
The class itself is only the description. The things you build from it are called objects or instances, and the next lesson covers creating them. This lesson is only about writing the blueprint.
The syntax
The smallest possible class is an empty one:
class Dog {
}
It has three parts:
- The keyword
classtells the compiler that a new type is being declared. - The name, here
Dog, is yours to choose. C# convention is PascalCase for class names: each word starts with a capital letter and there are no underscores, as inDog,BankAccountorHighScore. - The curly braces hold the body of the class. They can be empty. A class with no members is still a valid type, and you can create objects from it.
Names in C# are case-sensitive, so Dog and dog are different identifiers. If the lesson asks for a class called Dog, a class called dog doesn't count, and neither does DOG.
Where classes go
A class is declared at the top level of the file, next to other classes, not inside a method. The lesson's Solution class is one such class, and Dog sits beside it. C# compiles the whole file together, so the order of the classes doesn't matter: Solution can use Dog whether Dog is written above or below it. Putting the small helper classes first, as the reference solution does, is just a readable habit.
Each class normally lives in its own file in a real project, but one file may hold many classes, and that is how the lesson works.
Adding data: a preview
An empty class can't do much. To make it hold data, you add members. You can use a field, which a later lesson covers. C# programmers more often use an auto-property:
class Dog {
public string Name { get; set; }
public int Age { get; set; }
}
The { get; set; } part tells C# to create the hidden storage and the code that reads and writes it. You use the property like a variable:
Dog dog = new Dog();
dog.Name = "Rex";
dog.Age = 4;
Console.WriteLine(dog.Name + " " + dog.Age); // Rex 4
Property names are PascalCase too. Don't worry about the details yet; the following lessons introduce fields, constructors and methods one at a time.
Common mistakes
- Misspelling or changing the case of the name. The code that uses
Dogmust match the declaration exactly. A class nameddogleads to a compile error such as "The type or namespace name 'Dog' could not be found". - Forgetting the braces.
class Dogon its own is incomplete. The body is required, even when it is empty. - Declaring the class inside a method. The class belongs at the top level of the file, outside
Solutionand its methods. - Mixing up the class and an object.
Dogis the type. A variable such asdogholds one object of that type. You can't writeDog.Name = "Rex"for a normal property, because the name belongs to an individual dog, not to the blueprint. - Using underscores or lowercase names.
bank_accountcompiles, but it doesn't follow C# conventions, and other C# programmers expectBankAccount.
The exercise, step by step
The lesson gives you a Solution class whose MainFunction already contains this code:
Dog dog = new Dog();
Console.WriteLine("Defined the Dog class!");
That code doesn't compile yet, because there is no type called Dog. Your only job is to declare one.
- Write the declaration above
Solution:class Dog, then a pair of braces with nothing inside. - Leave
MainFunctionalone. WithDogdefined, the linenew Dog()creates an instance of your empty class, and the next line printsDefined the Dog class!.
class Dog {
}
class Solution {
public void MainFunction() {
Dog dog = new Dog();
Console.WriteLine("Defined the Dog class!");
}
}
The program prints:
Defined the Dog class!
Key takeaways
- A class is a blueprint for a new type; objects are built from it.
- The syntax is the keyword
class, a PascalCase name and a pair of braces. - An empty body is valid.
- Classes are declared at the top level of the file, and their order doesn't matter.
- Data and behaviour go inside the braces, using fields, properties and methods, which later lessons introduce.