← Back to C# Course | Chapter 6: OOP Basics | Lesson 1 of 7

Classes

A class is a blueprint, like the plan for a house or the design of a toy. It describes what things of that type know and can do.

In this page:

  1. Classes
Syntax
csharp
class ClassName
{
    public type Field;

    public void MethodName()
    {
        // uses Field
    }
}

Classes

A class is a blueprint that bundles data (fields) and behavior (methods) into a single type. You define it once and create as many independent objects from it as you need. Classes are the foundation of object-oriented programming in C#.

Note: Class names conventionally use PascalCase, like BankAccount.

Example: Classes

markup
using System;

class Dog
{
    public string Name;
    public int Age;

    public void Bark()
    {
        Console.WriteLine($"{Name} says Woof!");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Dog d = new Dog();
        d.Name = "Rex";
        d.Age = 3;
        d.Bark();
    }
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Confusing a class (the blueprint) with an object (an instance).
  2. Forgetting to mark members public and then being unable to reach them from other classes, since members are private by default.
  3. Putting too many unrelated responsibilities into one class.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.