Implicit (int → double) happens automatically. Explicit: (int) 9.7, Convert.ToInt32("42"), int.Parse("42"), or the safe int.TryParse().
💡 Key Takeaways: C# Variables & Data Types
Mastering C# Variables & Data Types helps you write clean and reliable C# code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.
C# Variables & Data Types Code Example
C#
int age = 25;
decimal price = 1999.99m;
bool active = true;
char grade = 'A';
string name = "Sara";
var city = "Karachi";
const double TaxRate = 0.18;
Console.WriteLine($"{name} ({age}) from {city}, grade {grade}, active {active}");
Console.WriteLine($"Price with tax: {price * (decimal)(1 + TaxRate):F2}");
int parsed = int.Parse("42");
bool ok = int.TryParse("abc", out int bad);
Console.WriteLine($"{parsed + 8} {ok} {bad}");
Console.WriteLine((int) 9.7);
Output
Sara (25) from Karachi, grade A, active True
Price with tax: 2359.99
50 False 0
9
Run this example on your own computer. The "Get Started" lesson shows how to set it up.
C# Variables & Data Types Practice Exercises
Check what you learned in “C# Variables & Data Types”.
/ 2 solved
Fill in the blank
Use the exact type for money.
price = 1999.99m;
✓ Correct!Not quite. Try again.
Multiple choice
Which method converts safely and returns false on bad input?