C# Tutorial: Variables and Types
Learn value types, reference types, constants, type conversion, and nullability in C#.
Value vs Reference Types
C# has two categories of types:
**Value types** store data directly:
```csharp
int x = 10;
bool flag = true;
char letter = 'A';
double price = 9.99;
```
**Reference types** store a reference (pointer) to data:
```csharp
string name = "Alice";
int[] numbers = { 1, 2, 3 };
```
---
Constants
Use `const` for compile-time constants:
```csharp
const double Pi = 3.14159;
const string AppName = "uByte";
```
Use `readonly` for values set once at runtime:
```csharp
readonly DateTime startTime = DateTime.Now;
```
---
Type Conversion
**Implicit** (safe, no data loss):
```csharp
int i = 42;
long l = i; // int → long (widening)
double d = i; // int → double
```
**Explicit cast** (may lose precision):
```csharp
double pi = 3.99;
int truncated = (int)pi; // 3
```
**Parse from string**:
```csharp
int n = int.Parse("42");
bool ok = int.TryParse("abc", out int result); // ok = false, safe
```
---
Nullable Types
Add `?` to allow a value type to hold `null`:
```csharp
int? age = null;
if (age.HasValue) {
Console.WriteLine(age.Value);
}
// Null-coalescing: use default when null
int display = age ?? 0;
```
---
What's Next?
You know the type system. Next: **print formatting** — `Console.Write`, `Console.WriteLine`, string interpolation, and format specifiers.
What you'll learn in this C# variables and types tutorial
This interactive C# tutorial has 5 hands-on exercises. Estimated time: 12 minutes.
- Value Types — Declare an `int` named `age` with value `25`, a `double` named `price` with value `9.99`, and a `bool` named `isActive` …
- var Inference — C# can infer types with `var`. Declare `var greeting = "Hello"` and `var count = 42`. Print them on separate lines.
- Constants — Declare a `const double Pi = 3.14159` and print `"Pi is 3.14159"`.
- Explicit Type Conversion — Declare `double pi = 3.99` and cast it to `int` using `(int)pi`. Print the result (it should be `3` — truncated, not rou…
- Nullable Types — Declare `int? score = null`. Use the null-coalescing operator `??` to print `0` when `score` is null.
C# Variables and Types concepts covered
- Value vs Reference Types
- Constants
- Type Conversion
- Nullable Types
- What's Next?