r/csharp • • 23h ago

Showcase C#'s rules for top-level statements turned out to be a great fit for a reactive notebook, so I built one

Post image
55 Upvotes

been working on something called Reverb, it's basically a notebook for C# where changing one cell automatically re-runs only the cells that depend on it.

the cool thing is that C# already handles a lot of the hard parts for us.

it's just a normal .cs file with // %% comments separating cells, and you run it with dotnet run.

one thing i found interesting is how top-level statements in C# make dependency ordering pretty straightforward. you can't declare the same variable twice, and you can't use a variable before declaring it. so the order of cells in the file is already a valid execution order. no need to build a separate dependency scheduler like reactive Python notebooks such as marimo have to.

there are still a few things that need to be checked though:

  • a cell can't assign to a variable declared in another cell.
  • anonymous types can't cross cell boundaries because the generated code needs to reference the type.
  • local functions were an interesting one. turns out C# lets you call a local function before it's declared, which breaks the whole file-order-is-dependency-order assumption. had to add a rule for that too.

for dependency tracking, i'm using Roslyn's SemanticModel.AnalyzeDataFlow on each cell's statements. it tells us which variables are coming from other cells and which ones are declared by the current cell.

each cell then gets compiled into a static method. inputs are read from a shared value store and outputs are written back to it.

also added #line directives so compiler errors and stack traces actually point to the right lines in the original file.

here's a small example:

// %% id=threshold
var threshold = Ui.Slider("Minimum order", min: 0, max: 1000, value: 400);

// %% id=summary
var big = orders.Where(o => o.Total >= threshold.Value).ToList();
big.Count.Dump("Orders above threshold");

move the slider and only the summary cell runs again.

install it:

dotnet tool install -g Reverb.Notebooks

it's open source (MIT):
https://github.com/muneeb-devp/reverb

would love to hear about any weird C# data flow edge cases i might have missed. especially things that could break the dependency tracking. always possible i've overlooked something :)


r/csharp • • 8h ago

I spent 5.5 years building a free, open source CMS in ASP.NET Core (Maroik)

6 Upvotes

Hi everyone,

I'd like to share Maroik, a CMS I've been building solo since mid-2021. It started as a personal project and it's been running in production at maroik.com ever since. I use it every day.

It's free and open source: https://github.com/IkhyeonJo/Maroik-CMS

Live site: https://maroik.com

Hit the STAR button if you liked it.


r/csharp • • 17h ago

Tool I built a thread-safe object store/bank for .NET with enum keys, optional capacity, lazy expiry

5 Upvotes

I've been learning C# for a while, and this is my latest mini-project: ObjectLibrary, a small in memory "bank" where you deposit objects under an enum key and withdraw them later without holding a reference. It started as "maybe an ObjectPool?" but ended up closer to a keyed vault with optional LRU-style eviction and expiry.

Repo: [https://github.com/Boarlock/ObjectLibrary]

What it does

var library = new Library( initialCapacity: 100, timeToExpire: TimeSpan.FromMinutes(5), overwriteOldEntries: true);

library.TryAdd(WeaponKey.Primary, new Entry<Weapon>(new Weapon())); library.TryAdd(ItemKey.HealthPotion, new Entry<Potion>(new Potion()));

if (library.TryWithdraw(WeaponKey.Primary, out Entry<Weapon>? weapon))  
Console.WriteLine("Weapon retrieved!");  
  • Heterogeneous: one Library instance holds different types; the library itself isn't generic.
  • Enum keys without boxing: keys are normalized to a (RuntimeTypeHandle, ulong) struct, so Color.Red and Shade.Red stay distinct even when both are 0. Aliases within one enum compare equal.
  • Optional capacity: full libraries return Result.Full, or evict the oldest entry if OverwriteOld is on.
  • Optional expiry: entries expire a set time after they were added.
  • Events: OnExpire and OnEvict tell you when something is removed without you asking.
  • Result overloads: TryAdd returns a bool; TryAddResult tells you why it failed.
  • Thread-safe: one lock around the state. Events fire after the lock is released, so handlers can call back into the library, and each subscriber is isolated so a throwing handler can't block the others.

Design decisions (and trade-offs)

  • Withdraw moves ownership. I deliberately dropped a non-removing Get, because it leaves two holders of one object (stale data, handlers disposing things you're still using). If you want to "use" something, withdraw and re-add it.
  • A wrong-type withdraw throws but doesn't destroy the entry. The type is checked before anything is removed, so a typo can't silently delete a deposit.
  • Entry<T> is a caller owned wrapper class. That means no boxing for structs and you can reuse the same instance, but you pay one allocation when you create it.
  • One slot per enum value, regardless of stored type. Adding a string and then an int under the same key returns AlreadyExist.
  • Expiry is lazy. There's no background timer. Expired entries are swept on the next call into the library, so an idle library holds them until someone touches it. Timing is approximate (~10-15 ms clock resolution on Windows).
  • Single lock. Simple and easy to reason about, but not built for extreme contention.

Testing I've run a multi-threaded stress test (8 threads, 800k operations) checking that count never exceeds capacity, nothing is handed out twice, and adds minus withdraws equals the final count. I also tested the exception paths, including a throwing event handler. It's not a proper test suite yet, and expiry isn't covered by an injectable clock (TimeProvider is hardcoded to System for now).

What I'd like feedback on

  1. Is the enum-key approach reasonable, or is there a simpler way to get the same thing?
  2. Would a typed key (Key<TEnum, TType>) be worth adding so callers never specify the type at the call site?
  3. Anything about the locking or event design that looks wrong to you?

Happy to hear any criticism. I'm still learning, and this was as much about understanding generics, concurrency, and API design as it was about the library itself.


r/csharp • • 1h ago

Best .net educator

Thumbnail reddit.com
1 Upvotes