Understanding Properties, Fields, and Methods in C# Classes

Once you’ve learned how to create classes and objects in C#, the next step is to understand how to structure them properly using fields, properties, and methods. These are the key building blocks that give your objects both data and behaviour.

This is where we’ll explain the role of each — when to use them, how they interact, and how to write clean, maintainable class code.


🧱 Fields: The Private Variables Inside a Class

A field is a variable declared directly inside a class. It usually stores data related to the object, such as a name, age, or ID.

By default, fields are made private to protect the internal state of the object.

🔹 Example:

public class Person
{
    private string name;
    private int age;
}

🔐 Fields are often kept private so you can control access using properties.


🪟 Properties: The Public Interface to Private Data

A property allows controlled access to a private field. It typically includes a get and/or set method — sometimes with custom logic.

🔹 Auto-Implemented Property:

public string Name { get; set; }

This is shorthand for a private field with automatic get/set access.

🔸 Full Property with Backing Field:

private int age;

public int Age
{
    get { return age; }
    set
    {
        if (value >= 0)
            age = value;
    }
}

✅ This allows validation or transformation before storing a value.


⚙️ Methods: The Actions of the Class

Methods define the behaviour of your object — what it can do.

They can:

  • Modify internal fields or properties
  • Return data
  • Interact with other objects

🔹 Example:

public void Introduce()
{
    Console.WriteLine($"Hi, my name is {Name} and I am {Age} years old.");
}

🧪 Complete Example: Person Class

public class Person
{
    // Field
    private int age;

    // Property with validation
    public int Age
    {
        get { return age; }
        set
        {
            if (value >= 0)
                age = value;
        }
    }

    // Auto property
    public string Name { get; set; }

    // Method
    public void Greet()
    {
        Console.WriteLine($"Hello! I'm {Name}, and I'm {Age}.");
    }
}

🔸 Using the class:

Person p = new Person();
p.Name = "Alice";
p.Age = 28;
p.Greet();

🧠 When to Use Each

ElementPurposeAccessibility
FieldStores private data internallyprivate
PropertyExposes data with control/validationpublic
MethodPerforms an action or logicpublic/private

💡 Follow the principle of encapsulation — hide internal data using private fields, and expose safe access through public properties and methods.


🧰 Best Practices

  • ✅ Use auto-properties for simple get/set cases
  • ✅ Use backing fields for validation or logic
  • ✅ Keep fields private unless there’s a strong reason not to
  • ✅ Use meaningful method names like CalculateSalary() or PrintInvoice()

🧪 Quick Exercise

Create a Book class with:

  • A private price field
  • A Price property that doesn’t allow negative numbers
  • A Title auto-property
  • A method DisplayDetails() that prints the book’s title and price

📚 Summary

ConceptRole in Class
FieldStores internal object data
PropertyControls access to fields
MethodEncapsulates logic or actions

📬 Need help structuring your classes or applying validation logic with properties? Contact us — we’re here to help you learn modern C# the right way.