Understanding C# Classes: A Beginner's Guide with Examples

C# is a popular object-oriented programming language that offers a lot of flexibility in terms of creating custom data types. One of the fundamental building blocks of C# is the class. A class is a blueprint that defines the structure and behavior of an object. In this beginner's guide, we will cover the basics of C# classes and explore how they can be used to create custom data types. 

Constructors 
A constructor is a special method that is used to initialize an object of a class. It has the same name as the class and is executed automatically when an object of that class is created. Constructors can be used to set initial values for the fields of an object or to perform any other necessary initialization tasks. 

Fields 
Fields are the variables that belong to a class. They define the state of an object and can be accessed and modified from within the class. Fields are declared at the beginning of a class and can have different access modifiers (public, private, protected, etc.) that determine who can access them. 

Properties 
Properties provide a way to access and modify the fields of an object in a controlled way. They are defined by a pair of get and set accessors that specify how the property's value should be retrieved and assigned. Properties can have different access modifiers, just like fields. 

Methods 
Methods are the functions that belong to a class. They define the behavior of an object and can be called to perform specific tasks. Methods can have parameters that allow them to accept input and return values that provide output. 

Here's an example of a simple class in C#:
public class Person
{
    private string name;
    private int age;

    public Person(string name, int age)
    {
        this.name = name;
        this.age = age;
    }

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    public int Age
    {
        get { return age; }
        set { age = value; }
    }

    public void SayHello()
    {
        Console.WriteLine("Hello, my name is " + name + " and I am " + age + " years old.");
    }
}
In this example, we have created a class called Person that has two fields (name and age) and three methods (the constructor, Name property, Age property, and SayHello method). The constructor takes two parameters (name and age) and initializes the corresponding fields. The Name and Age properties provide controlled access to the name and age fields, and the SayHello method prints a message to the console. 

C# classes are a powerful feature that allows developers to create custom data types that can encapsulate both data and behavior. By understanding the basics of classes, including constructors, fields, properties, and methods, beginners can start creating their own classes and building more complex applications.