C
C#

C to C#

10 lessons

Progress0%
1Variables & Types2Functions & Methods3Arrays & Lists4Structs → Classes5Memory Management6Strings7File I/O8Object-Oriented Programming9Generics and Collections10Async and Concurrency
All Mirror Courses
C
C#
Variables & Types
MirrorLesson 1 of 10
Lesson 1

Variables & Types

Declaring and using variables

Introduction

In this lesson, you'll learn about variables & types in C#. Coming from C, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.

Mirror Card
C
From C:

In C, you're familiar with declaring and using variables.

C#
In C#:

C# has its own approach to declaring and using variables, which we'll explore step by step.

The C# Way

Let's see how C# handles this concept. Here's a typical example:

C#
C# Example
using System;

int age = 30;
float score = 9.5f;
double pi = 3.14159;
char letter = 'A';
const int Max = 100;
bool active = true; // real bool type

// var — type inference
var name = "Alice"; // inferred as string

Console.WriteLine(string.Format("age={0}, pi={1:F2}", age, pi));

Comparing to C

Here's how you might have written similar code in C:

C
C (What you know)
#include <stdio.h>

int main() {
    int age = 30;
    float score = 9.5f;
    double pi = 3.14159;
    char letter = 'A';
    const int MAX = 100;
    int active = 1; /* no bool */

    printf("age=%d, pi=%.2f\n", age, pi);
    return 0;
}
Mirror Card
C
From C:

You may be used to different syntax or behavior.

C#
In C#:

C# has a real bool type (true/false); C uses int

Mirror Card
C
From C:

You may be used to different syntax or behavior.

C#
In C#:

C# var keyword infers types like C's auto; C requires explicit types

Mirror Card
C
From C:

You may be used to different syntax or behavior.

C#
In C#:

C# const is compile-time; C's const is merely read-only

Mirror Card
C
From C:

You may be used to different syntax or behavior.

C#
In C#:

C# has nullable types (int?) for optional values

Step-by-Step Breakdown

1. Boolean Type

C# has a dedicated bool type with true/false literals. C uses int (0 for false, non-zero for true).

C
C
int active = 1;
C#
C#
bool active = true;

2. Type Inference

C#'s var keyword infers the type from the right-hand side, similar to C's __auto_type or C++'s auto.

C#
C#
var name = "Alice";    // string
var count = 42;        // int
var items = new List<string>();

3. Nullable Types

C# nullable value types (int?) can represent the absence of a value — replacing the C pattern of using -1 or a sentinel value.

C#
C#
int? maybeAge = null;
if (maybeAge.HasValue)
    Console.WriteLine(maybeAge.Value);

Common Mistakes

When coming from C, developers often make these mistakes:

  • C# has a real bool type (true/false); C uses int
  • C# var keyword infers types like C's auto; C requires explicit types
  • C# const is compile-time; C's const is merely read-only
Common Pitfall
Don't assume C# works exactly like C. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • C# bool (true/false) replaces C int 0/1
  • var infers types like C++ auto
  • C# const is compile-time constant
  • Nullable types (int?) represent missing values
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your C code in C# to practice these concepts.
OverviewNext