Properties vs fields
📖 Scenario: Imagine you are creating a simple program to manage a book's information. You want to store the book's title and control how it is accessed and changed.
🎯 Goal: You will create a class with a field and a property, then see how to use them to get and set the book's title.
📋 What You'll Learn
Create a class called
Book with a private field called _title of type stringAdd a public property called
Title with get and set accessors to control access to _titleCreate an instance of
Book and set the Title propertyPrint the value of the
Title property💡 Why This Matters
🌍 Real World
Properties help control how data inside objects is accessed and changed, which is important for keeping data safe and consistent in real programs.
💼 Career
Understanding properties and fields is essential for writing clean, maintainable code in C# and many other object-oriented languages.
Progress0 / 4 steps