Start learning free

LESSON 179 OF 347 · OBJECT-ORIENTED PROGRAMMING

Defining a Class in C#: Syntax, Naming and First Steps

Lesson Read the full explanation

TL ; DR:

  • A class is a blueprint for creating objects. It groups together related data and behavior under a single name.


  • In C#, you define a class with the class keyword, followed by the class name and a pair of curly braces:

    class Dog {
    
    }
    

    The body of the class can be empty — that's still a valid class. We'll add data and behavior to it in the next lessons.



Full lesson:

So far, you've worked with built-in types like int, string, and arrays. But what if you want to model something the language doesn't have a type for — like a Dog, a BankAccount, or a Game?

That's what classes are for. A class lets you define your own type by listing what data it holds and what actions it can perform.


Why classes?

Imagine you're writing a program about dogs. Each dog has a name, an age, and can bark. Without classes, you'd have to track each dog's name in one variable, age in another, and write a separate bark() function for each. That gets messy fast.

With a class, you bundle all of that into one neat package called Dog:

class Dog {
    // We'll add fields (data) and methods (behavior) here in upcoming lessons.
}

Now whenever your program needs a dog, it asks for one of these Dog objects, and the class makes sure each one has all the right pieces.


The syntax

Defining an empty class in C# looks like this:

class ClassName {
}

Three things to notice:

  • The keyword class tells C# you're defining a new type.

  • ClassName is the name you choose. By convention, C# class names use PascalCase — capitalized words with no underscores: Dog, BankAccount, HighScore.

  • The curly braces { } hold the body of the class. They can be empty for now.

You don't need to do anything inside the class to make it valid — defining it is enough.


Where do classes go?

You can define classes at the top level of your file, right alongside the Solution class that's already there. The order doesn't matter — C# can find Dog whether it's defined above or below the code that uses it.


Properties:

To store data in a class, C# doesn't use public fields with hand-written getters and setters — it uses auto-properties. The { get; set; } shorthand tells C# to generate the backing storage and accessors for you:

class Dog {
    public string Name { get; set; }
    public int Age { get; set; }
}

You'll meet this { get; set; } shorthand in the next lessons and throughout the coding tutorials wherever a class needs to hold data.


Your task

Define an empty class called Dog. The pre-filled code in MainFunction will create an instance of your class and print a confirmation message. If you define the class correctly, the program will compile and print:

Defined the Dog class!
STEP 1 OF 1
    Ctrl + Enter to check

    C# · OBJECT-ORIENTED PROGRAMMING

    Defining A Class in C#, explained

    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 class tells 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 in Dog, BankAccount or HighScore.
    • 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 Dog must match the declaration exactly. A class named dog leads to a compile error such as "The type or namespace name 'Dog' could not be found".
    • Forgetting the braces. class Dog on 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 Solution and its methods.
    • Mixing up the class and an object. Dog is the type. A variable such as dog holds one object of that type. You can't write Dog.Name = "Rex" for a normal property, because the name belongs to an individual dog, not to the blueprint.
    • Using underscores or lowercase names. bank_account compiles, but it doesn't follow C# conventions, and other C# programmers expect BankAccount.

    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.

    1. Write the declaration above Solution: class Dog, then a pair of braces with nothing inside.
    2. Leave MainFunction alone. With Dog defined, the line new Dog() creates an instance of your empty class, and the next line prints Defined 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.
    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free