Library vs framework explained simply: programming is clear instructions, you call a library, and a framework calls your code
Library vs framework is a classic beginner and interview question, but to answer it well we first need a clear idea of what programming is. In this lesson of the C# series we do both, with a recipe, a trigonometry book and one line of C#.
What is programming?
Programming means writing clear instructions for a computer. First you understand the problem, break it into small steps (step 1, step 2, step 3), and then write the instructions for each step in a programming language.
A computer is very fast but very literal: it follows the instructions exactly and never guesses what you meant.
Programming is like a recipe
| In the kitchen | In programming | |—|—| | Recipe | Program | | Ingredients (dough, cheese, onion) | Input | | Cooking steps | Instructions (code) | | The chef | The computer | | The finished pizza | Output |
Write the recipe once (take the dough, add the toppings, bake for 40 minutes) and any chef can follow it. Write the program once and the computer can run it.
Why the computer needs a compiler
Hardware only understands machine code: 0s and 1s. A line like if (a > b) print(a) means nothing to the processor directly.
In C#, the compiler turns your code into an intermediate language (IL), and when the program runs, .NET’s just-in-time compiler turns that into machine code the hardware can execute. Programming languages exist so humans can write instructions in a human-friendly way.
Library vs framework: who is in charge?
The short answer: you call a library; a framework calls you. This is called inversion of control.
- Library: ready-made code you pick from. You are in charge: you decide when to call it. Like a library of books: to solve a trigonometry problem you take the trigonometry book and use its method; you don’t reinvent trigonometry.
- Framework: a foundation with tools built in. The framework is in charge: it provides the structure and environment, and calls your code at the right moments. .NET gives us this kind of ready environment for C# programs.
A real library call in C#
System.Text.Json is a library that ships with .NET. To turn an object into JSON you just call its function:
using System.Text.Json;
var student = new { Name = "Sunil", Age = 16 };
string json = JsonSerializer.Serialize(student);
Console.WriteLine(json); // {"Name":"Sunil","Age":16}
You don’t need to know how it works behind the scenes; you only need to know what the function does. The same goes for a payment gateway: you use the provider’s library instead of writing it from scratch.
Interview answers in one line
- What is programming? Giving precise instructions to a computer.
- What is a library? Code you call when you need it.
- What is a framework? A structure that invokes your code.
Video chapters
- 0:00 Programming, libraries and frameworks
- 0:19 What is programming?
- 2:29 Programming is like a recipe
- 5:48 Hardware only understands 0 and 1
- 7:07 Library vs framework: who is in charge?
- 8:18 A library: the trigonometry book
- 9:51 Example: System.Text.Json
- 10:35 Interview answers
- 11:38 Wrap-up
Watch the short versions
Short on time? These Shorts from this lesson each make one point in a minute or so.
- Programming Is Like a Recipe (Explained Simply) (Short, coming soon)
- Library vs Framework: The One-Line Interview Answer (Short, coming soon)
Key takeaways
- Programming = clear, precise instructions, after breaking the problem into small steps.
- Hardware only runs machine code; the compiler (and .NET’s JIT) translate your C#.
- Library vs framework: you call a library; a framework calls your code (inversion of control).
- Libraries like System.Text.Json save you from rewriting solved problems.
Frequently asked questions
Is .NET a library or a framework?
.NET is a platform that includes a runtime, a large class library (like System.Text.Json) and frameworks such as ASP.NET Core, where the framework calls your code.
Is React a library or a framework?
React calls itself a library, but in practice it controls when your components render, which is why the two terms are often mixed up. The test is the same: who calls whom?
Why can’t the computer understand C# directly?
The processor only executes machine code (0s and 1s); the compiler and runtime translate your C# into it.
Further reading
- InversionOfControl (Martin Fowler): the ‘who calls whom’ idea behind frameworks
- How to serialize JSON in C# (Microsoft Learn): System.Text.Json and JsonSerializer.Serialize
- Managed execution process (Microsoft Learn): from C# to IL to machine code
- The difference between a framework and a library (freeCodeCamp): a beginner-friendly explanation
Practice: name one library and one framework you have used (or heard of) and say who calls whom.
Next in the series: what is .NET?
