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

String interpolation and formatting in C Sharp (C#) - Mini Project: Build & Apply

Choose your learning style9 modes available
String interpolation and formatting
📖 Scenario: You are creating a simple program to display product information in a store. You want to show the product name, quantity, and price in a clear and friendly way.
🎯 Goal: Build a program that uses string interpolation and formatting to display product details with price shown as currency and quantity as a number.
📋 What You'll Learn
Create variables for product name, quantity, and price
Use a format string with string interpolation to combine these variables
Format the price as currency
Format the quantity as a number without decimals
Print the final formatted string
💡 Why This Matters
🌍 Real World
Displaying product information clearly in shopping apps or inventory systems helps customers and staff understand prices and stock easily.
💼 Career
Knowing how to format strings and numbers is essential for creating user-friendly interfaces and reports in software development.
Progress0 / 4 steps
1
Create product variables
Create a string variable called productName and set it to "Chocolate Bar". Create an integer variable called quantity and set it to 5. Create a double variable called price and set it to 1.99.
C Sharp (C#)
Need a hint?

Use string for text, int for whole numbers, and double for decimal numbers.

2
Create a formatted message variable
Create a string variable called message that uses string interpolation to combine productName, quantity, and price. Format price as currency using :C and quantity as a number with no decimals using :N0 inside the interpolation.
C Sharp (C#)
Need a hint?

Use $"...{variable:format}..." to insert variables with formatting.

3
Add a method to create the message
Create a method called CreateMessage that returns a string. Inside it, return the same formatted string using productName, quantity, and price with the same formatting as before. Use the existing variables from the outer scope.
C Sharp (C#)
Need a hint?

Define a method with string CreateMessage() and return the formatted string inside.

4
Print the formatted message
Use Console.WriteLine to print the message variable. Then print the result of calling the CreateMessage() method.
C Sharp (C#)
Need a hint?

Use Console.WriteLine(message); and Console.WriteLine(CreateMessage()); to print.