Object-oriented programming (OOP) is a cornerstone of modern software development, and C# is a language built from the ground up to support it. Understanding the fundamental concepts of classes and objects is crucial for writing maintainable, scalable, and efficient C# code. This lesson will provide a comprehensive introduction to defining classes, creating objects (also known as instantiation), and how these concepts form the basis for more advanced OOP principles. Since you're new to OOP, we'll start with the basics and build a solid foundation.

Defining Classes

A class is a blueprint or a template for creating objects. It defines the characteristics (data) and behaviors (methods) that objects of that class will have. Think of it like a cookie cutter – the class is the cutter, and the objects are the cookies.

Class Syntax

In C#, you define a class using the class keyword, followed by the name of the class and a pair of curly braces {}. Inside the curly braces, you define the members of the class, which can include fields (variables) and methods (functions).

// Defining a simple class named 'Dog'
class Dog
{
    // Fields (data)
    public string name;
    public string breed;
    public int age;

    // Method (behavior)
    public void Bark()
    {
        Console.WriteLine("Woof!");
    }
}

Class Members: Fields and Methods

Classes contain members, which are the variables (fields) and functions (methods) that define the class's characteristics and behavior.

Access Modifiers

Access modifiers control the visibility and accessibility of class members. Common access modifiers include: