PY
C#

Python to C#

10 lessons

Progress0%
1Variables & Types2Classes & OOP3Collections & LINQ4Async/Await5Exception Handling6File I/O7Generics8Delegates and Events9Records and Pattern Matching10Interfaces
All Mirror Courses
PY
C#
Collections & LINQ
MirrorLesson 3 of 10
Lesson 3

Collections & LINQ

Working with sequences and dictionaries

Introduction

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

Mirror Card
PY
From Python:

In Python, you're familiar with working with sequences and dictionaries.

C#
In C#:

C# has its own approach to working with sequences and dictionaries, 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;

// List<T>
var nums = new List<int> { 1, 2, 3, 4, 5 };
nums.Add(6);

// Dictionary<K,V>
var ages = new Dictionary<string, int> {
    ["Alice"] = 30, ["Bob"] = 25
};
ages["Charlie"] = 35;

// LINQ — like comprehensions
var doubled = nums.Select(n => n * 2).ToList();
var evens = nums.Where(n => n % 2 == 0).ToList();
int total = nums.Sum();

// Dictionary from LINQ
var squares = Enumerable.Range(0, 5)
    .ToDictionary(n => n, n => n * n);

// Destructuring
var (first, second) = (nums[0], nums[1]);

Comparing to Python

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

PY
Python (What you know)
# List
nums = [1, 2, 3, 4, 5]
nums.append(6)

# Dict
ages = {"Alice": 30, "Bob": 25}
ages["Charlie"] = 35

# List comprehensions
doubled = [n * 2 for n in nums]
evens = [n for n in nums if n % 2 == 0]
total = sum(nums)

# Dict comprehension
squares = {n: n**2 for n in range(5)}

# Unpacking
first, *rest = nums
Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

C#
In C#:

Python list → C# List<T> (generic)

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

C#
In C#:

Python dict → C# Dictionary<K,V>

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

C#
In C#:

Python list/dict comprehensions → C# LINQ (Select/Where/ToDictionary)

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

C#
In C#:

Python sum() → C# .Sum(); Python len() → .Count

Step-by-Step Breakdown

1. Generic Collections

C# collections use generics (List<int>) to enforce element types at compile time. Python lists can hold anything.

PY
Python
nums = [1, 2, 3]; nums.append(4)
C#
C#
var nums = new List<int> { 1, 2, 3 };
nums.Add(4);

2. LINQ vs Comprehensions

Python list comprehensions become LINQ method chains. Select=map, Where=filter, ToList() materializes the result.

PY
Python
[n * 2 for n in nums if n > 2]
C#
C#
nums.Where(n => n > 2).Select(n => n * 2).ToList()

3. Dictionary

C# Dictionary uses typed keys and values. The collection initializer syntax with [] closely resembles Python dict literals.

PY
Python
d = {"key": 1}; d.get("key", 0)
C#
C#
var d = new Dictionary<string, int> { ["key"] = 1 };
d.GetValueOrDefault("key", 0);

Common Mistakes

When coming from Python, developers often make these mistakes:

  • Python list → C# List<T> (generic)
  • Python dict → C# Dictionary<K,V>
  • Python list/dict comprehensions → C# LINQ (Select/Where/ToDictionary)
Common Pitfall
Don't assume C# works exactly like Python. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • Python list → C# List<T>
  • Python dict → C# Dictionary<K,V>
  • List comprehensions → LINQ (Select/Where/ToList)
  • sum/len → .Sum()/.Count
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your Python code in C# to practice these concepts.
PreviousNext