0
0
C Sharp (C#)programming~15 mins

Computed properties in C Sharp (C#) - Mini Project: Build & Apply

Choose your learning style9 modes available
Computed Properties in C#
📖 Scenario: You are creating a simple program to manage a rectangle's dimensions and calculate its area automatically.
🎯 Goal: Build a C# class with computed properties that calculate the area of a rectangle based on its width and height.
📋 What You'll Learn
Create a class called Rectangle with two properties: Width and Height.
Add a computed property called Area that returns the product of Width and Height.
Create an instance of Rectangle with specific width and height values.
Print the value of the Area property.
💡 Why This Matters
🌍 Real World
Computed properties are useful in many programs where you want to calculate values automatically without storing extra data.
💼 Career
Understanding computed properties helps you write cleaner, more efficient code in C# applications, a common skill in software development jobs.
Progress0 / 4 steps
1
Create the Rectangle class with Width and Height properties
Create a public class called Rectangle with two public auto-implemented properties: Width and Height, both of type double.
C Sharp (C#)
Need a hint?

Use public double Width { get; set; } and similarly for Height.

2
Add the computed property Area
Inside the Rectangle class, add a public computed property called Area of type double that returns the product of Width and Height using only a getter.
C Sharp (C#)
Need a hint?

Use expression-bodied property syntax: public double Area => Width * Height;

3
Create an instance of Rectangle with specific dimensions
In the Main method, create a variable called rect and assign it a new Rectangle object with Width set to 5.0 and Height set to 3.0.
C Sharp (C#)
Need a hint?

Use object initializer syntax: new Rectangle { Width = 5.0, Height = 3.0 }

4
Print the Area property of the Rectangle instance
In the Main method, add a Console.WriteLine statement to print the value of rect.Area.
C Sharp (C#)
Need a hint?

Use Console.WriteLine(rect.Area); to display the area.