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#
Arrays & Lists
MirrorLesson 3 of 10
Lesson 3

Arrays & Lists

Working with sequences of data

Introduction

In this lesson, you'll learn about arrays & lists 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 working with sequences of data.

C#
In C#:

C# has its own approach to working with sequences of data, 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.Collections.Generic;
using System.Linq;

// Fixed array
int[] nums = { 1, 2, 3, 4, 5 };
int n = nums.Length;

// Dynamic list (no malloc/free)
var list = new List<int> { 1, 2, 3 };
list.Add(4);
list.AddRange(new[] { 5, 6 });
list.Remove(3);

// Foreach
foreach (int x in nums) {
    Console.WriteLine(x);
}

// LINQ — functional operations
int[] squared = nums.Select(x => x * x).ToArray();
int[] evens = nums.Where(x => x % 2 == 0).ToArray();
int total = nums.Sum();

Comparing to C

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

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

/* Fixed array */
int nums[5] = {1, 2, 3, 4, 5};
int n = sizeof(nums)/sizeof(nums[0]);

/* Dynamic array — manual */
int *dyn = malloc(10 * sizeof(int));
dyn[0] = 42;
dyn = realloc(dyn, 20 * sizeof(int));
free(dyn);

/* Iteration */
for (int i = 0; i < n; i++) {
    printf("%d\n", nums[i]);
}
Mirror Card
C
From C:

You may be used to different syntax or behavior.

C#
In C#:

C# List<T> is the dynamic array — no malloc/free needed

Mirror Card
C
From C:

You may be used to different syntax or behavior.

C#
In C#:

C# arrays have .Length property; C needs sizeof division trick

Mirror Card
C
From C:

You may be used to different syntax or behavior.

C#
In C#:

C# foreach replaces C's index-based for loop

Mirror Card
C
From C:

You may be used to different syntax or behavior.

C#
In C#:

LINQ provides functional map/filter/reduce with no C equivalent

Step-by-Step Breakdown

1. List vs malloc

C#'s List<T> is a type-safe, garbage-collected dynamic array. It grows automatically — no malloc, realloc, or free.

C
C
int *dyn = malloc(10 * sizeof(int));
dyn = realloc(dyn, 20 * sizeof(int));
free(dyn);
C#
C#
var list = new List<int>();
list.Add(1);
// grows automatically, GC frees it

2. foreach

C#'s foreach iterates any IEnumerable without index management, reducing off-by-one errors common in C.

C
C
for (int i = 0; i < n; i++) printf("%d\n", arr[i]);
C#
C#
foreach (int x in nums) Console.WriteLine(x);

3. LINQ

LINQ adds SQL-like querying and functional operations (Select=map, Where=filter, Aggregate=reduce) to all collections.

C#
C#
var result = nums
    .Where(x => x > 2)
    .Select(x => x * x)
    .ToList();

Common Mistakes

When coming from C, developers often make these mistakes:

  • C# List<T> is the dynamic array — no malloc/free needed
  • C# arrays have .Length property; C needs sizeof division trick
  • C# foreach replaces C's index-based for loop
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

  • List<T> replaces malloc/realloc/free for dynamic arrays
  • GC handles memory; no manual cleanup
  • foreach over for(i=0;i<n;i++) for clarity
  • LINQ provides functional query operations
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your C code in C# to practice these concepts.
PreviousNext