C# supports both value types (int, double, bool, char, struct) and reference types (string, class, array, object).
int age = 25;
double price = 19.99;
string name = "Alice";
bool isActive = true;
var number = 42; // Type inferred as int
// Implicit (safe)
int num = 100;
double d = num;
// Explicit (may lose data)
double pi = 3.14;
int truncated = (int)pi;
// Convert class
string input = "42";
int parsed = Convert.ToInt32(input);
int parsed2 = int.Parse(input);
Console.Write("Enter your name: ");
string name = Console.ReadLine();
Console.Write("Enter your age: ");
int age = int.Parse(Console.ReadLine());
// Arithmetic: +, -, *, /, %
// Comparison: ==, !=, <, >, <=, >=
// Logical: &&, ||, !
// Assignment: =, +=, -=, *=, /=, ++, --
// Math: Math.Pow, Math.Sqrt, Math.Max, Math.Min, Math.Round
if (age >= 18) {
Console.WriteLine("Adult");
} else if (age >= 13) {
Console.WriteLine("Teen");
} else {
Console.WriteLine("Child");
}
int day = 3;
switch (day) {
case 1: Console.WriteLine("Monday"); break;
case 2: Console.WriteLine("Tuesday"); break;
default: Console.WriteLine("Other"); break;
}
// For loop
for (int i = 0; i < 5; i++) {
Console.WriteLine(i);
}
// While loop
int count = 0;
while (count < 5) {
Console.WriteLine(count);
count++;
}
// Do-While
int x = 0;
do {
Console.WriteLine(x);
x++;
} while (x < 5);
string greeting = "Hello";
Console.WriteLine(greeting.Length);
Console.WriteLine(greeting.ToUpper());
Console.WriteLine(greeting.Contains("ell"));
bool isComplete = true;
bool isGreater = 10 > 5; // true
Write a C# program that applies the concepts covered in this chapter. Experiment with different inputs and test your understanding.