Skip to content
Variables and Types
step 1/6

Reading — step 1 of 6

Learn

~3 min readBasics

Variables and Types

C# is statically typed: every variable has a type that is fixed at compile time, and the compiler checks every use of it before your program ever runs. This is a deliberate trade — you write a little more up front, and in exchange whole categories of bugs (adding a number to a date, calling a method that does not exist) are caught instantly instead of exploding at runtime.

int age = 30;                      // 32-bit integer
long population = 8000000000L;     // 64-bit integer — much bigger range
double pi = 3.14159;               // 64-bit floating point
decimal price = 9.99m;             // high-precision decimal — use for money
bool isActive = true;              // true or false
string name = "Ada";               // immutable Unicode text
char initial = 'A';                // exactly one character, single quotes

Two rules of thumb worth learning early:

  • double is the default for decimals, but it has binary rounding error (0.1 + 0.2 is not exactly 0.3). For money, use decimal (note the m suffix on the literal).
  • string uses double quotes; char uses single quotes. They are different types.

var asks the compiler to infer the type from the right-hand side:

var count = 42;        // count is int — decided at compile time, forever
var label = "hi";      // label is string

var is not "dynamic typing" — count is an int for the rest of its life. It just saves you from writing the type name twice.

C# also splits types into value types (int, double, bool, structs — copied on assignment) and reference types (string, arrays, classes — assignment shares the same underlying object). You will feel this distinction much more in later lessons; for now, just know it exists.

Reading input: the classic trap

Console.ReadLine() always returns a string, even when the user types a number. To do math, you must parse:

// Idiom: parse each line into an int, then add
int a = int.Parse(Console.ReadLine());
int b = int.Parse(Console.ReadLine());
Console.WriteLine(a + b);          // 3 and 4 -> 7
// Trap: adding the strings instead of the numbers
string a = Console.ReadLine();
string b = Console.ReadLine();
Console.WriteLine(a + b);          // "3" + "4" -> "34"  (concatenation!)

+ on strings means "glue together". Forgetting to parse is the single most common first-week C# bug — the program runs fine and prints confidently wrong answers.

When input might be garbage, the safe form is int.TryParse(s, out int n): it returns false on bad input instead of throwing an exception, and writes the parsed value into n on success. int.Parse is fine here, where the grader promises clean input.

Your exercise

Read two integers from stdin — each arrives on its own line — and print their sum. The starter already parses both lines into int a and int b; you add the line that prints a + b.

The grader will catch exactly the trap above: concatenate strings instead of adding ints and input 3, 4 prints 34 instead of 7. Print only the number — 7, not Sum: 7 — with Console.WriteLine(a + b);. The hidden test feeds -5 and 5 and expects 0, so negative numbers must work too (they do, automatically, once you have real ints).

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…