<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/quack-quack-duck-typing-in-c-with-interceptors-ii9dc4vvn" -->

---
title: Quack Quack: Duck-Typing in C# with Interceptors | daily.dev
description: An experimental C# compiler feature called interceptors, available since .NET 8, is used to emulate TypeScript-style structural typing (duck typing) without...
canonical: https://daily.dev/posts/quack-quack-duck-typing-in-c-with-interceptors-ii9dc4vvn
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Quack Quack: Duck-Typing in C# with Interceptors | daily.dev
og:description: An experimental C# compiler feature called interceptors, available since .NET 8, is used to emulate TypeScript-style structural typing (duck typing) without...
og:url: https://daily.dev/posts/quack-quack-duck-typing-in-c-with-interceptors-ii9dc4vvn
og:image: https://api.daily.dev/og/posts/ii9dc4Vvn.png
og:image:alt: Quack Quack: Duck-Typing in C# with Interceptors
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.

# Quack Quack: Duck-Typing in C# with Interceptors

**[We Are .NET](https://daily.dev/sources/wearedotnet)** · 4 min read · 12 upvotes · 1 comments

## Summary

An experimental C# compiler feature called interceptors, available since .NET 8, is used to emulate TypeScript-style structural typing (duck typing) without touching the original classes or using dynamic, object casting, or shared interfaces. Custom attributes ([DuckShape] and [DuckTyped]) drive a source generator that emits interceptor methods and tiny adapter structs at each call site, wrapping the concrete type to satisfy the declared shape interface at compile time with zero runtime reflection cost, only a small dispatch overhead. The technique works for both methods and properties, and the author frames it as an educational exercise rather than a production-recommended pattern, with full code linked on GitHub.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://steven-giesel.com/blogPost/5170e165-29e8-437a-b5ce-446a84943809>

## Questions this post answers

### How can I make a C# method accept two unrelated classes that both have the same method signature, without a shared interface?

Use the experimental interceptors compiler feature introduced in .NET 8 combined with a source generator. Mark an interface with a custom [DuckShape] attribute to declare the required structural shape, then mark the consuming method with [DuckTyped]. The generator emits an [InterceptsLocation] method per call site that wraps the concrete argument in a small generated adapter struct implementing that interface, forwarding calls to the real object at zero runtime reflection cost.

_daily.dev surfaces deep dives like this for developers exploring advanced .NET compiler tricks and source generators._

### What is the interceptors feature in C# and how does it work at compile time?

Interceptors is an experimental C# compiler feature, available since .NET 8, that lets a source generator emit a method tagged with [InterceptsLocation] pointing at a specific call site, causing the compiler to silently redirect that call to the generated method instead. The swap happens entirely at compile time, so there is no runtime cost beyond the generated code itself.

_Developers tracking experimental .NET compiler features can follow this kind of source-generator technique on daily.dev._

## Community discussion

Top comments from developers on daily.dev.

**@sevensolutions** · 0 upvotes

> Dont give them bad ideas 😂. Nowadays they put every crap into the C# language.

## Similar posts on daily.dev

- [If It Quacks - Part 2](https://daily.dev/posts/if-it-quacks---part-2-pk5hxtn0w) · We Are .NET · 0 upvotes · 0 comments
- [Meet Duck Typer: your new duck typing friend](https://daily.dev/posts/meet-duck-typer-your-new-duck-typing-friend-bkn7wnfxf) · RUBYLAND · 0 upvotes · 0 comments
- [Duck Typing and Inheritance in Ruby](https://daily.dev/posts/duck-typing-and-inheritance-in-ruby-q0pcc61at) · Ruby Flow · 0 upvotes · 0 comments
- [Duck Typing in Python: Writing Flexible and Decoupled Code Quiz – Real Python](https://daily.dev/posts/duck-typing-in-python-writing-flexible-and-decoupled-code-quiz-real-python-ryls9aksb) · Real Python · 0 upvotes · 0 comments

---

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

[View this post on daily.dev](https://daily.dev/posts/quack-quack-duck-typing-in-c-with-interceptors-ii9dc4vvn)

```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":"Quack Quack: Duck-Typing in C# with Interceptors","url":"https://daily.dev/posts/quack-quack-duck-typing-in-c-with-interceptors-ii9dc4vvn","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/quack-quack-duck-typing-in-c-with-interceptors-ii9dc4vvn"},"datePublished":"2026-09-12T16:36:25.057Z","dateModified":"2026-09-12T16:37:18.862Z","description":"An experimental C# compiler feature called interceptors, available since .NET 8, is used to emulate TypeScript-style structural typing (duck typing) without...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/65532c0a89198f087f652d0d2785e406?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/65532c0a89198f087f652d0d2785e406?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"We Are .NET","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":"We Are .NET","logo":"https://media.daily.dev/image/upload/s--BsnVOa3i--/f_auto/v1718347410/logos/wearedotnet","url":"https://daily.dev/sources/wearedotnet"},"commentCount":1,"discussionUrl":"https://daily.dev/posts/quack-quack-duck-typing-in-c-with-interceptors-ii9dc4vvn","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":12},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":1}],"keywords":".net,c#","timeRequired":"PT4M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"We Are .NET","item":"https://daily.dev/sources/wearedotnet"},{"@type":"ListItem","position":3,"name":"Quack Quack: Duck-Typing in C# with Interceptors"}]}
{"@context":"https://schema.org","@type":"WebPage","@id":"https://daily.dev/posts/quack-quack-duck-typing-in-c-with-interceptors-ii9dc4vvn","comment":[{"@type":"Comment","text":"Dont give them bad ideas 😂. Nowadays they put every crap into the C# language.","datePublished":"2026-09-21T07:04:37.342Z","url":"https://daily.dev/posts/ii9dc4Vvn#c-9PPLHYGOy","author":{"@type":"Person","name":"Daniel Peinhopf","url":"https://daily.dev/sevensolutions","image":"https://avatars.githubusercontent.com/u/84123899?v=4"}}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/quack-quack-duck-typing-in-c-with-interceptors-ii9dc4vvn#faq","mainEntity":[{"@type":"Question","name":"How can I make a C# method accept two unrelated classes that both have the same method signature, without a shared interface?","acceptedAnswer":{"@type":"Answer","text":"Use the experimental interceptors compiler feature introduced in .NET 8 combined with a source generator. Mark an interface with a custom [DuckShape] attribute to declare the required structural shape, then mark the consuming method with [DuckTyped]. The generator emits an [InterceptsLocation] method per call site that wraps the concrete argument in a small generated adapter struct implementing that interface, forwarding calls to the real object at zero runtime reflection cost. daily.dev surfaces deep dives like this for developers exploring advanced .NET compiler tricks and source generators."}},{"@type":"Question","name":"What is the interceptors feature in C# and how does it work at compile time?","acceptedAnswer":{"@type":"Answer","text":"Interceptors is an experimental C# compiler feature, available since .NET 8, that lets a source generator emit a method tagged with [InterceptsLocation] pointing at a specific call site, causing the compiler to silently redirect that call to the generated method instead. The swap happens entirely at compile time, so there is no runtime cost beyond the generated code itself. Developers tracking experimental .NET compiler features can follow this kind of source-generator technique on daily.dev."}}]}
```

