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
EasyLet'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 anItemclass in theInventorynamespace with auto-implemented properties forName(string),Price(decimal), andInStock(bool). Set default values:Nameshould default to"Unknown",Priceto0.0m, andInStocktotrue.Program.cs: In your main file, create twoItemobjects 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: TrueCheat 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}
}
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Object Oriented Programming
1Fundamentals of OOP
External FilesNamespaces & DirectivesIntro to Classes & ObjectsThe 'this' KeywordMethods and ParametersFields vs PropertiesConstructorsObject InitializersRecap - Simple Calculator4Inheritance
Basic Inheritance (:) SyntaxThe 'base' KeywordVirtual & Override KeywordsSealed ClassesThe 'object' Base ClassRecap - Employee Hierarchy7Advanced Features
Operator OverloadingIndexers (this[])ToString() OverrideExtension MethodsRecap - Custom List2Properties & Static Members
Auto-Implemented PropertiesRead/Write-Only PropertiesStatic Fields & MethodsStatic ClassesExpression-Bodied Members5Polymorphism & Interfaces
Compile vs Runtime PolyInterface vs Abstract ClassMultiple InterfacesExplicit InterfacesUpcasting & DowncastingRecap - Shape Calculator