Understanding Properties and Methods
Explore how objects use properties and methods to store data and perform actions.
Understanding Properties and Methods is a free C# Academy lesson on CoddyKit — lesson 3 of 7. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 7 lessons in the course, and your progress syncs across the web and the CoddyKit app.
1
Understanding Properties and Methods
Welcome to the next lesson! In this lesson, you’ll learn about properties and methods, their purpose, and how to use them effectively in C#. Let’s get started!

2
What Are Properties?
Properties provide a way to encapsulate and control access to a class’s fields. They use get and set accessors to retrieve and modify field values.
Example: Properties ensure that the field data is validated or computed dynamically.
3
Using Properties
Properties are often used to validate or process data before assigning it to a field or returning its value. Example:
using System;
class Person {
private int age;
public int Age {
get { return age; }
set {
if (value > 0) {
age = value; // Only assign valid ages
} else {
Console.WriteLine("Age must be positive.");
}
}
}
}
class Program {
static void Main() {
Person person = new Person();
person.Age = 25; // Valid value
Console.WriteLine("Age: " + person.Age);
person.Age = -5; // Invalid value
}
}
4
Auto-Implemented Properties
For simple cases, you can use auto-implemented properties. Example:
Tip: Auto-implemented properties simplify the code when no additional logic is needed.
using System;
class Car {
public string Model { get; set; }
public string Color { get; set; }
}
class Program {
static void Main() {
Car car = new Car { Model = "Sedan", Color = "Blue" };
Console.WriteLine("Model: " + car.Model + ", Color: " + car.Color);
}
}
5
What Are Methods?
Methods define the behavior of a class. They are blocks of reusable code that perform specific tasks, improving code modularity and readability.
Example: A method could calculate the area of a rectangle or print a greeting message.
6
Defining and Calling Methods
To define a method, specify the return type, method name, and optional parameters. Example:
Key Point: Methods can return values or perform actions without returning anything (void).
using System;
class Calculator {
public int Add(int a, int b) {
return a + b; // Adds two numbers
}
}
class Program {
static void Main() {
Calculator calc = new Calculator();
int result = calc.Add(10, 5); // Call the method
Console.WriteLine("Sum: " + result);
}
}
7
Methods with Parameters
Methods can accept parameters to operate on different values. Example:
Tip: Parameters allow you to pass data into methods for greater flexibility.
using System;
class Greeter {
public void Greet(string name) {
Console.WriteLine("Hello, " + name + "!");
}
}
class Program {
static void Main() {
Greeter greeter = new Greeter();
greeter.Greet("Alice");
greeter.Greet("Bob");
}
}
8
Practice Challenge
Create a class called Rectangle with properties Length and Width, and a method CalculateArea() that returns the area of the rectangle. Example:
using System;
class Rectangle {
public int Length { get; set; }
public int Width { get; set; }
public int CalculateArea() {
return Length * Width;
}
}
class Program {
static void Main() {
Rectangle rect = new Rectangle { Length = 5, Width = 4 };
Console.WriteLine("Area: " + rect.CalculateArea());
}
}
9
10
Great Job!
Congratulations! You’ve learned how to define and use properties and methods to encapsulate and add functionality to your classes. In the next lesson, we’ll explore constructors and learn how to initialize objects effectively. Let’s keep coding!

Frequently asked questions
Is the “Understanding Properties and Methods” lesson free?
Yes — the full text of “Understanding Properties and Methods” is free to read here on the web, and the C# Academy course includes 7 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “Understanding Properties and Methods”?
Explore how objects use properties and methods to store data and perform actions. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 7, so you can start here or from the beginning and move at your own pace.
How long does the “Understanding Properties and Methods” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.