<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/net-developer-personal-blog-6p46s51nn" -->

---
title: .NET Developer, Personal Blog | daily.dev
description: A lock-free DateTime cache in .NET 11 gets slightly faster by swapping the reference-type cache item for a struct packed into a long[], but the author says not...
canonical: https://daily.dev/posts/net-developer-personal-blog-6p46s51nn
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: .NET Developer, Personal Blog | daily.dev
og:description: A lock-free DateTime cache in .NET 11 gets slightly faster by swapping the reference-type cache item for a struct packed into a long[], but the author says not...
og:url: https://daily.dev/posts/net-developer-personal-blog-6p46s51nn
og:image: https://api.daily.dev/og/posts/6p46S51nN.png
og:image:alt: .NET Developer, Personal Blog
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# .NET Developer, Personal Blog

**[Laszlo Deak](https://daily.dev/sources/ladeak)** · 5 min read · 0 upvotes · 0 comments

## Summary

A lock-free DateTime cache in .NET 11 gets slightly faster by swapping the reference-type cache item for a struct packed into a long[], but the author says not to use it in production. Keys sit in indices 0 to 29 and DateTime values at an offset of 30. Unsafe.BitCast turns DateTime into a long so Interlocked.CompareExchange works. An unrepresentable tick value for year 10000 marks empty slots. The assembly loop has one fewer cmp instruction and one less dereference. The author concludes the unsafe code is not worth the gain.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <http://blog.ladeak.net/posts/improving-datetime-cache>

## Questions this post answers

### How can I use Interlocked.CompareExchange to cache DateTime values in .NET 11 without a lock?

Interlocked.CompareExchange does not work on custom structs, so represent both the year and the DateTime as long values. Unsafe.BitCast<long, DateTime> converts between them. Store keys at indices 0 to 29 and values at 30 to 59 of a long[], using an invalid DateTime tick value as the empty-slot marker. The code is unsafe and not for production.

_Developers tuning .NET concurrency follow low-level performance experiments like this on daily.dev._

### Why does bit casting a long to DateTime in .NET 11 produce values that new DateTime() would reject?

Unsafe.BitCast<long, DateTime> bypasses DateTime's precondition checks, so it can create values the constructor refuses. For example, 3652059 * TimeSpan.TicksPerDay corresponds to 01/01/10000, yet new DateTime(10000, 1, 1) throws ArgumentOutOfRangeException. That unrepresentable value can serve as a sentinel meaning no entry has been set.

_Keeping up with .NET internals and unsafe tricks is easier when you follow them on daily.dev._

## Similar posts on daily.dev

- [How to Build a High-Performance Cache Without External Libraries](https://daily.dev/posts/how-to-build-a-high-performance-cache-without-external-libraries-phts4izvp) · Milan Jovanović · 6 upvotes · 0 comments
- [Dating in Hungarian](https://daily.dev/posts/dating-in-hungarian-zgmrw0o1s) · The Daily WTF · 7 upvotes · 0 comments
- [Cache Me If You Can: A .NET Caching Survival Guide](https://daily.dev/posts/cache-me-if-you-can-a-net-caching-survival-guide-sydnkz4ko) · ITNEXT · 2 upvotes · 0 comments
- [Locking and Concurrency Control in .NET 10](https://daily.dev/posts/locking-and-concurrency-control-in-net-10-um1rhzp52) · Waseem .NET Newsletter · 14 upvotes · 0 comments
- [Stop Pretending TimeProvider Doesn't Exist — Daily DevOps & .NET](https://daily.dev/posts/stop-pretending-timeprovider-doesn-t-exist-daily-devops-net-8lez0ga1i) · We Are .NET · 21 upvotes · 1 comments

---

Tags: [#performance](https://daily.dev/tags/performance), [#.net](https://daily.dev/tags/.net), [#c#](https://daily.dev/tags/c#)

[View this post on daily.dev](https://daily.dev/posts/net-developer-personal-blog-6p46s51nn)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":".NET Developer, Personal Blog","url":"https://daily.dev/posts/net-developer-personal-blog-6p46s51nn","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/net-developer-personal-blog-6p46s51nn"},"datePublished":"2026-12-01T00:05:18.116Z","dateModified":"2026-12-01T00:05:40.034Z","description":"A lock-free DateTime cache in .NET 11 gets slightly faster by swapping the reference-type cache item for a struct packed into a long[], but the author says not...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/9b0ec3a95676d7dd7e16d628432bd93a?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/9b0ec3a95676d7dd7e16d628432bd93a?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Laszlo Deak","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Laszlo Deak","logo":"https://media.daily.dev/image/upload/s--Uolv4Tlk--/f_auto,q_auto/v1791119054/logos/ladeak?_a=BAMAMicg0","url":"https://daily.dev/sources/ladeak"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/net-developer-personal-blog-6p46s51nn","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"performance,.net,c#","timeRequired":"PT5M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Laszlo Deak","item":"https://daily.dev/sources/ladeak"},{"@type":"ListItem","position":3,"name":".NET Developer, Personal Blog"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/net-developer-personal-blog-6p46s51nn#faq","mainEntity":[{"@type":"Question","name":"How can I use Interlocked.CompareExchange to cache DateTime values in .NET 11 without a lock?","acceptedAnswer":{"@type":"Answer","text":"Interlocked.CompareExchange does not work on custom structs, so represent both the year and the DateTime as long values. Unsafe.BitCast\u003clong, DateTime\u003e converts between them. Store keys at indices 0 to 29 and values at 30 to 59 of a long[], using an invalid DateTime tick value as the empty-slot marker. The code is unsafe and not for production. Developers tuning .NET concurrency follow low-level performance experiments like this on daily.dev."}},{"@type":"Question","name":"Why does bit casting a long to DateTime in .NET 11 produce values that new DateTime() would reject?","acceptedAnswer":{"@type":"Answer","text":"Unsafe.BitCast\u003clong, DateTime\u003e bypasses DateTime's precondition checks, so it can create values the constructor refuses. For example, 3652059 * TimeSpan.TicksPerDay corresponds to 01/01/10000, yet new DateTime(10000, 1, 1) throws ArgumentOutOfRangeException. That unrepresentable value can serve as a sentinel meaning no entry has been set. Keeping up with .NET internals and unsafe tricks is easier when you follow them on daily.dev."}}]}
```

