C# notebooks are useful for experimenting with code, testing APIs, exploring data, and learning new .NET features without creating a complete application.
Traditional notebook formats, however, can introduce some practical problems when they are used in development workflows.
One common issue is stale output. When notebook cells are executed out of order, the displayed result may no longer represent the current state of the code. Another problem is version control. Many notebook systems store cells and their outputs in structured JSON files, which can make Git diffs difficult to review.
An alternative approach is to use ordinary C# source files and divide them into executable cells.
This article explores that approach and looks at how a C# notebook system can provide reactive execution, dependency tracking, interactive controls, package management, and other notebook capabilities while keeping the underlying source code as a normal .cs file.
What Is a C# Notebook?
A C# notebook allows developers to write and execute small pieces of C# code independently.
Instead of creating a complete project for every experiment, developers can write code such as:
var numbers = new[] { 10, 20, 30, 40 };
var total = numbers.Sum();
Console.WriteLine(total);The code can be divided into multiple cells:
Cell 1
↓
Define data
Cell 2
↓
Transform data
Cell 3
↓
Display resultEach cell can then be executed independently.
This workflow is useful for:
Learning C#
Exploring .NET APIs
Testing algorithms
Querying data
Prototyping ideas
Testing libraries
Exploring APIs
Creating technical demonstrations
The Problem with Traditional Notebook Workflows
Notebook environments are convenient, but their execution model can cause problems.
Consider two cells:
// %%
var price = 100;and:
// %%
var tax = price * 0.18;
Console.WriteLine(tax);If the first cell is changed but the second cell is not executed again, the visible output may represent an earlier state.
The notebook can therefore contain code that says one thing while the displayed output represents another execution state.
This becomes particularly confusing when a notebook contains many cells and developers execute them in different orders.
Using Plain C# Source Files
A different approach is to store notebook code as a regular .cs file.
Cells can be separated using a marker such as:
// %%For example:
// %%
var price = 100;
// %%
var tax = price * 0.18;
// %%
Console.WriteLine($"Tax: {tax}");The important difference is that the notebook is still a normal C# source file.
This provides several advantages for development workflows:
Git can display normal source-code diffs
Developers can edit the file using standard text editors
Code remains easy to search
The file can be stored alongside other source files
AI coding tools can work with the file
Developers are not tied to a JSON notebook format
Reactive Cell Execution
A useful improvement over simple cell execution is dependency-aware execution.
Suppose a notebook contains:
// %%
var price = 100;
// %%
var tax = price * 0.18;
// %%
var total = price + tax;The dependency relationship is:
price
↓
tax
↓
totalIf price changes, the cells that depend on it need to be recalculated.
A reactive notebook can analyze these relationships and execute only the affected cells.
For example:
Change price
|
v
Recalculate tax
|
v
Recalculate totalThis avoids manually rerunning unrelated cells.
Dependency Analysis with Roslyn
The C# compiler platform, Roslyn, provides APIs for analyzing C# source code.
A notebook system can use Roslyn's syntax and semantic analysis capabilities to determine relationships between cells.
For example, if one cell defines:
var customer = GetCustomer();and another contains:
Console.WriteLine(customer.Name);the second cell depends on the value created by the first cell.
A dependency-aware execution engine can use this information to determine which cells need to be evaluated again when code changes.
This is different from simply executing cells from top to bottom.
Handling Deleted Cells
Dependency tracking also becomes important when a cell is deleted.
Consider:
// %%
var customerName = "Alex";
// %%
Console.WriteLine(customerName);If the first cell is removed, customerName should no longer be available to the second cell.
A notebook execution engine therefore needs to maintain an accurate representation of the current source state rather than preserving variables from previous executions.
This helps prevent stale variables and stale output from remaining in the notebook.
Interactive Controls
Notebook environments can also provide interactive controls.
For example, a notebook could expose a slider for a numeric value:
Discount: [--------|---]Changing the value can trigger dependent cells to execute again.
Similarly, dropdowns and text inputs can be used to provide parameters to notebook code.
This can be useful for demonstrations and exploratory analysis.
For example:
Select product:
[ Laptop ▼ ]
Quantity:
[ 5 ]
Discount:
[ 10% ]The notebook can then recalculate the results when the inputs change.
Displaying Rich Results
A notebook does not have to display only console output.
A modern C# notebook environment can support different types of results, including:
Tables
Charts
Markdown
HTML
Images
Text output
For example, a query could return tabular data directly instead of requiring developers to print every row manually.
This makes notebooks useful for data exploration and technical demonstrations.
Using NuGet Packages
Another useful capability is the ability to add NuGet packages directly from a notebook.
A package declaration can look like:
#:package [email protected]The notebook can then use APIs from that package without requiring developers to create a complete project manually.
This is particularly useful when experimenting with:
JSON libraries
HTTP clients
Database libraries
Data processing libraries
Machine learning libraries
.NET APIs
Package management also makes notebooks more reproducible because dependencies can be declared alongside the code that uses them.
Running Notebooks as Applications
Some notebook environments can take the same C# notebook and run it as an application.
For example:
reverb run hello.csThe important idea is that the notebook source remains the same while the execution mode changes.
This can make notebooks useful for small tools, demonstrations, and prototypes rather than limiting them to interactive experimentation.
Exporting Notebook Results
Notebook content can also be useful outside the development environment.
A notebook system can export results to formats such as HTML or convert existing notebook formats such as .ipynb or .dib.
This allows developers to use notebooks for documentation and demonstrations while retaining the underlying C# source.
For example:
C# source
|
+---- Interactive notebook
|
+---- HTML documentation
|
+---- Executable workflowWorking with Editors and AI Coding Tools
Because the notebook is stored as a regular .cs file, it can fit naturally into existing development tools.
A developer can work with the file using editors such as:
Visual Studio Code
Vim
Other text editors
AI coding agents
This is an important distinction from notebook formats that require specialized editors.
An AI coding agent can also inspect and modify the notebook as ordinary C# source code.
That makes the format easier to integrate into existing Git-based development workflows.
Performance Considerations
Reactive execution introduces additional analysis because the system needs to determine which cells depend on changed code.
A practical implementation therefore needs to balance correctness and execution speed.
A notebook engine can improve responsiveness by:
Keeping compiler state warm
Reusing previous analysis results
Re-executing only affected cells
Avoiding unnecessary compilation
Maintaining dependency information between cells
For interactive development, keeping edit-to-result latency low is important because long delays can make experimentation difficult.
Current Limitations
The plain .cs notebook approach is promising, but there are still areas that require further development.
For example:
C# may be the only supported language.
Editor integration may be limited.
Mutation tracking can be difficult.
Complex dependency relationships require careful analysis.
External state can make deterministic execution harder.
Side effects can complicate reactive re-execution.
The last point is particularly important.
Consider:
// %%
database.Insert(customer);Automatically rerunning this cell could insert the same customer multiple times.
Therefore, dependency tracking alone is not enough for a fully reliable reactive notebook. The execution engine also needs to understand side effects and state.
Plain .cs Files vs JSON Notebooks
Feature | Plain | JSON Notebook |
|---|---|---|
Git diffs | Easy to review | Can be noisy |
Source editing | Standard text editor | Usually notebook editor |
C# tooling | Natural fit | Depends on environment |
AI coding tools | Easy to integrate | May require notebook-specific support |
Cell execution | Supported through markers | Native notebook model |
Rich outputs | Can be supported | Commonly supported |
Dependency tracking | Can be implemented | Depends on notebook engine |
Portability | High for C# source | Depends on notebook format |
The choice depends on the development workflow. JSON-based notebooks remain useful when rich interactive documents and multiple languages are important, while plain C# files offer a simpler source-control and development experience.
When Plain C# Notebooks Make Sense
This approach is particularly useful when developers want the flexibility of notebooks without moving away from normal source-code workflows.
Good use cases include:
C# experimentation
Data analysis
API exploration
Learning exercises
Algorithm testing
Documentation examples
Small prototypes
.NET feature exploration
Reproducible technical demonstrations
For larger production applications, however, a conventional project structure remains more appropriate.
Conclusion
C# notebooks do not necessarily need a special JSON-based file format.
Using ordinary .cs files with cell markers provides a simple foundation for combining interactive development with traditional source-code workflows.
The more interesting part is the execution model. Dependency-aware execution can prevent stale outputs, while Roslyn-based analysis can help determine which cells need to be recalculated when source code changes.
Additional features such as interactive controls, rich output, NuGet package support, HTML export, and application execution can turn a simple C# source file into a useful development and experimentation environment.
The approach also fits naturally with Git, standard editors, and AI coding tools. For developers who prefer working directly with C# source files, this provides an interesting alternative to traditional notebook workflows.


Join the conversation! Your thoughts help the community grow.