Menu
Coddy logo textTech

Fields vs Properties

Part of the Object Oriented Programming section of Coddy's C# journey — lesson 6 of 70.

Fields are variables declared directly in a class. Properties are members that provide controlled access to fields using get and set accessors.

Fields store data directly

public class Person
{
    // Fields - direct variable storage
    private string name;
    private int age;
}

Properties provide controlled access with get and set

public class Person
{
    private string name;
    
    // Property with backing field
    public string Name
    {
        get { return name; }
        set { name = value; }
    }
}

Auto-implemented properties (shorthand)

public class Person
{
    // Auto-implemented property
    public string Name { get; set; }
    public int Age { get; set; }
}

Properties can include validation logic

public class Person
{
    private int age;
    
    public int Age
    {
        get { return age; }
        set 
        { 
            if (value >= 0 && value <= 120)
                age = value;
        }
    }
}

Read-only property (only get accessor)

public class Circle
{
    private double radius;
    
    public double Radius
    {
        get { return radius; }
    }
    
    // Read-only calculated property
    public double Area
    {
        get { return Math.PI * radius * radius; }
    }
}

Fields are typically private and accessed through properties. Properties allow you to add logic, validation, and control over how data is accessed or modified. Auto-implemented properties are convenient when you don't need extra logic.

challenge icon

Challenge

Medium

Create a BankAccount class that demonstrates the difference between fields and properties:

  • A private balance field to store the account balance
  • An auto-implemented property AccountName
  • A read-only property Balance that returns the balance field
  • Methods Deposit and Withdraw that modify the private field

Cheat sheet

Fields are variables declared directly in a class, while properties provide controlled access to fields using get and set accessors.

Fields store data directly:

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

Properties provide controlled access with get and set:

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

Auto-implemented properties (shorthand):

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

Properties with validation logic:

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

Read-only property (only get accessor):

public double Area
{
    get { return Math.PI * radius * radius; }
}

Fields are typically private and accessed through properties. Properties allow you to add logic, validation, and control over how data is accessed or modified.

Try it yourself

using System;

class Program
{
    static void Main()
    {
        string accountName = Console.ReadLine();
        decimal initialBalance = decimal.Parse(Console.ReadLine());
        decimal depositAmount = decimal.Parse(Console.ReadLine());
        
        BankAccount account = new BankAccount(accountName, initialBalance);
        
        Console.WriteLine($"Account: {account.AccountName}");
        Console.WriteLine($"Initial Balance: ${account.Balance}");
        
        account.Deposit(depositAmount);
        Console.WriteLine($"After Deposit: ${account.Balance}");
        
        string result = account.Withdraw(2000);
        Console.WriteLine($"Withdraw attempt: {result}");
        Console.WriteLine($"Final Balance: ${account.Balance}");
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming