Skip to content

Commit 4530986

Browse files
committed
Add 01_Variables section
1 parent d2c9f22 commit 4530986

14 files changed

Lines changed: 232 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Variables
2+
3+
When you declare a variable or constant in a program, you must either specify its type or use the var keyword to let the compiler infer the type.
4+
After you declare a variable, you can't redeclare it with a new type, and you can't assign a value not compatible with its declared type.
5+
When you declare a constant, you must specify its type and value, and once you have defined it, the value cannot be mutated.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
3+
class Variables1 {
4+
public static void Main() {
5+
// TODO: Add the missing keyword.
6+
x = 5;
7+
8+
Console.WriteLine($"x has the value {x}");
9+
}
10+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
3+
class Variables2 {
4+
public static void Main() {
5+
// TODO: Change the line below to fix the compiler error.
6+
var x;
7+
8+
if (x == 10) {
9+
Console.WriteLine("x is ten!");
10+
} else {
11+
Console.WriteLine("x is not ten!");
12+
}
13+
}
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
3+
class Variables3 {
4+
public static void Main() {
5+
// TODO: Change the line below to fix the compiler error.
6+
int x;
7+
8+
Console.WriteLine($"Number {x}");
9+
}
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
3+
class Variables4 {
4+
public static void Main() {
5+
// TODO: Change the line below to fix the compiler error.
6+
const x = 42;
7+
8+
Console.WriteLine($"Constant {x}");
9+
}
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
class Variables5 {
4+
public static void Main() {
5+
// TODO: Change the line below to fix the compiler error.
6+
int x = 34;
7+
const int y = x + 8;
8+
9+
Console.WriteLine($"Number {y}");
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
class Variables6 {
4+
public static void Main() {
5+
// TODO: Fix the constant initialization.
6+
const int x;
7+
x = 42;
8+
9+
Console.WriteLine($"Number {x}");
10+
}
11+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
class Variables1 {
4+
public static void Main() {
5+
// Declaring variables requires specifying the type...
6+
int x = 5;
7+
// ...or using the `var` keyword.
8+
// var x = 5;
9+
10+
Console.WriteLine($"x has the value {x}");
11+
}
12+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
3+
class Variables2 {
4+
public static void Main() {
5+
// The easiest way to fix the compiler error is to initialize the
6+
// variable `x`. By setting its value to an integer, C# infers its type
7+
// as `int` which is the default type for integers.
8+
var x = 42;
9+
10+
// But we can enforce a type different from the default `int` by
11+
// specifying the type explicitly:
12+
// ushort x = 42;
13+
14+
if (x == 10) {
15+
Console.WriteLine($"x is ten!");
16+
} else {
17+
Console.WriteLine($"x is not ten!");
18+
}
19+
}
20+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
3+
class Variables3 {
4+
public static void Main() {
5+
// Reading uninitialized variables isn't allowed in C#!
6+
// Therefore, we need to assign a value first.
7+
int x = 42;
8+
9+
Console.WriteLine($"Number {x}");
10+
11+
// It is possible to declare a variable and initialize it later.
12+
// But it can't be used before initialization.
13+
int y;
14+
y = 42;
15+
Console.WriteLine($"Number {y}");
16+
}
17+
}

0 commit comments

Comments
 (0)