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

Expression-bodied methods in C Sharp (C#) - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Expression-Bodied Methods in C#
📖 Scenario: You are building a simple calculator class to perform basic math operations. You want to write clean and short methods using expression-bodied syntax.
🎯 Goal: Create a Calculator class with expression-bodied methods for addition, subtraction, multiplication, and division. Then use the class to calculate and display results.
📋 What You'll Learn
Create a class called Calculator
Add expression-bodied methods Add, Subtract, Multiply, and Divide
Each method takes two int parameters and returns an int
Use the methods to calculate results for given numbers
Print the results exactly as shown
💡 Why This Matters
🌍 Real World
Expression-bodied methods are used in real-world C# projects to write concise and readable code, especially for simple methods and properties.
💼 Career
Knowing expression-bodied methods helps you write clean code, which is valued in software development jobs and improves maintainability.
Progress0 / 4 steps
1
Create the Calculator class
Create a class called Calculator with no methods yet.
C Sharp (C#)
Need a hint?
Use the class keyword followed by the class name Calculator.
2
Add expression-bodied Add and Subtract methods
Inside the Calculator class, add two expression-bodied methods: int Add(int a, int b) => a + b; and int Subtract(int a, int b) => a - b;.
C Sharp (C#)
Need a hint?
Use the arrow => syntax to write short methods that return the result directly.
3
Add expression-bodied Multiply and Divide methods
Add two more expression-bodied methods inside Calculator: int Multiply(int a, int b) => a * b; and int Divide(int a, int b) => a / b;.
C Sharp (C#)
Need a hint?
Remember to use the arrow => syntax for these methods as well.
4
Use Calculator methods and print results
Create a Calculator object called calc. Use it to calculate calc.Add(10, 5), calc.Subtract(10, 5), calc.Multiply(10, 5), and calc.Divide(10, 5). Print each result on its own line exactly as: Add: 15, Subtract: 5, Multiply: 50, Divide: 2.
C Sharp (C#)
Need a hint?
Create an instance of Calculator and use Console.WriteLine with string interpolation to print results.