Menu
Coddy logo textTech

Auto-Implemented Properties

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

Auto-implemented properties let you declare properties without writing explicit backing fields. The compiler creates a hidden private field automatically behind the scenes.

Instead of writing this verbose code:

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

You can simply write:

public class Product
{
    public string Name { get; set; }
    public decimal Price { get; set; }
    public int Quantity { get; set; }
}

You can also set default values directly in the declaration:

public class Product
{
    public string Name { get; set; } = "Unnamed";
    public decimal Price { get; set; } = 0.0m;
    public bool IsAvailable { get; set; } = true;
}

Auto-implemented properties are ideal when you don't need validation or custom logic in the getter or setter. They keep your classes clean and reduce boilerplate code significantly.

challenge icon

Challenge

Easy

Let's build a simple inventory system using auto-implemented properties to keep track of items in a store.

You'll create two files to organize your code:

  • Item.cs: Define an Item class in the Inventory namespace with auto-implemented properties for Name (string), Price (decimal), and InStock (bool). Set default values: Name should default to "Unknown", Price to 0.0m, and InStock to true.
  • Program.cs: In your main file, create two Item objects using object initializers. Read the name and price for each item from input, then print each item's details.

You will receive four inputs:

  • First item's name
  • First item's price
  • Second item's name
  • Second item's price

Print each item in this format:

Item: {Name}, Price: {Price}, In Stock: {InStock}

For example, if the inputs are Laptop, 999.99, Mouse, and 25.50, the output should be:

Item: Laptop, Price: 999.99, In Stock: True
Item: Mouse, Price: 25.50, In Stock: True

Cheat sheet

Auto-implemented properties allow you to declare properties without writing explicit backing fields. The compiler automatically creates a hidden private field.

Basic syntax:

public class Product
{
    public string Name { get; set; }
    public decimal Price { get; set; }
    public int Quantity { get; set; }
}

Setting default values:

public class Product
{
    public string Name { get; set; } = "Unnamed";
    public decimal Price { get; set; } = 0.0m;
    public bool IsAvailable { get; set; } = true;
}

Auto-implemented properties are ideal when you don't need validation or custom logic in the getter or setter.

Try it yourself

using System;
using Inventory;

class Program
{
    public static void Main(string[] args)
    {
        // Read inputs for first item
        string name1 = Console.ReadLine();
        decimal price1 = Convert.ToDecimal(Console.ReadLine());
        
        // Read inputs for second item
        string name2 = Console.ReadLine();
        decimal price2 = Convert.ToDecimal(Console.ReadLine());
        
        // TODO: Create two Item objects using object initializers
        // Set the Name and Price properties from the input values
        
        // TODO: Print each item's details in the format:
        // Item: {Name}, Price: {Price}, In Stock: {InStock}
    }
}
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