C# Data Types - Real-Life Example
Real-Life Example
Here is a real-life example of using different data types. A shop program calculates and prints the total cost of a number of items:
Example
// Create variables of different data types
int items = 3;
double pricePerItem = 4.25;
double totalCost = items * pricePerItem;
char currency = '$';
// Print variables
Console.WriteLine("Number of items: " + items);
Console.WriteLine("Price per item: " + currency + pricePerItem);
Console.WriteLine("Total cost: " + currency + totalCost);
Why These Data Types?
Each variable uses the data type that fits its value best:
itemsis anint, because you count items in whole numbers. You cannot buy half an item.pricePerItemis adouble, because a price can have decimals, like4.25.totalCostis adouble, because anintmultiplied by adoublegives adouble.currencyis achar, because$is a single character.