<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/if-it-quacks---part-2-pk5hxtn0w" -->

---
title: If It Quacks - Part 2 | daily.dev
description: IfItQuacks is a new C# library that brings compile-time duck typing to the language using source generators and interceptors. Developers declare an interface,...
canonical: https://daily.dev/posts/if-it-quacks---part-2-pk5hxtn0w
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: If It Quacks - Part 2 | daily.dev
og:description: IfItQuacks is a new C# library that brings compile-time duck typing to the language using source generators and interceptors. Developers declare an interface,...
og:url: https://daily.dev/posts/if-it-quacks---part-2-pk5hxtn0w
og:image: https://api.daily.dev/og/posts/pK5hXtN0w.png
og:image:alt: If It Quacks - Part 2
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.

# If It Quacks - Part 2

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

## Summary

IfItQuacks is a new C# library that brings compile-time duck typing to the language using source generators and interceptors. Developers declare an interface, mark a method with [DuckTyped], and pass in any type that structurally matches - including anonymous types - without implementing the interface or using reflection or dynamic. The library preserves identity semantics (Equals, GetHashCode, ToString forward to the wrapped instance), supports generics, static abstract members, operators, events, indexers, delegates, and requires zero project setup beyond installing the package. A notable use case is 'instant stubs' for unit tests using anonymous types instead of mock objects.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://steven-giesel.com/blogPost/41599817-4457-441e-b874-1a5c67f4e7cc>

## Questions this post answers

### How do I do duck typing in C# without reflection or dynamic?

Use the IfItQuacks library, which adds compile-time duck typing via a source generator and interceptors. Declare an interface, mark a method parameter with [DuckTyped], and pass any type with matching members - no base class, no attributes, no implements clause required. The generator checks structural compatibility at compile time and routes calls through a generated adapter, producing an analyzer error if a type doesn't fit.

_daily.dev surfaces library releases like this for developers evaluating new C# patterns._

### Can I use anonymous types as stubs for unit tests in C#?

Yes, with the IfItQuacks library anonymous types satisfy any interface that structurally matches their members, so you can pass an anonymous object like new { Name = "Steven", Age = 90 } directly into a method expecting an interface such as IPerson, without writing a mock class. The library calls this pattern 'Instant Stubs'.

_Testing patterns like instant stubs are worth tracking for developers who write C# unit tests on daily.dev._

### Does duck typing in C# preserve object identity for Equals and GetHashCode?

Yes, in the IfItQuacks library, generated adapters forward Equals, GetHashCode, and ToString calls to the wrapped instance, so duck-typed values behave correctly in dictionaries and sets. The original instance can also be recovered explicitly using Duck.Unwrap, and two adapters wrapping the same instance are equal.

_daily.dev helps C# developers keep up with library behaviors like adapter identity semantics._

## Similar posts on daily.dev

- [Quack Quack: Duck-Typing in C\# with Interceptors](https://daily.dev/posts/quack-quack-duck-typing-in-c-with-interceptors-ii9dc4vvn) · We Are .NET · 12 upvotes · 1 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
- [Why Duck Typer?](https://daily.dev/posts/why-duck-typer--kg841khqj) · RUBYLAND · 0 upvotes · 0 comments

---

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

[View this post on daily.dev](https://daily.dev/posts/if-it-quacks---part-2-pk5hxtn0w)

```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":"If It Quacks - Part 2","url":"https://daily.dev/posts/if-it-quacks---part-2-pk5hxtn0w","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/if-it-quacks---part-2-pk5hxtn0w"},"datePublished":"2026-09-18T08:52:20.761Z","dateModified":"2026-09-18T09:57:06.188Z","description":"IfItQuacks is a new C# library that brings compile-time duck typing to the language using source generators and interceptors. Developers declare an interface,...","image":"https://media.daily.dev/image/upload/s--qPvKM23u--/f_auto/v1722860399/public/Placeholder%2009","thumbnailUrl":"https://media.daily.dev/image/upload/s--qPvKM23u--/f_auto/v1722860399/public/Placeholder%2009","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":0,"discussionUrl":"https://daily.dev/posts/if-it-quacks---part-2-pk5hxtn0w","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"testing,.net,c#","timeRequired":"PT3M"}
{"@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":"If It Quacks - Part 2"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/if-it-quacks---part-2-pk5hxtn0w#faq","mainEntity":[{"@type":"Question","name":"How do I do duck typing in C# without reflection or dynamic?","acceptedAnswer":{"@type":"Answer","text":"Use the IfItQuacks library, which adds compile-time duck typing via a source generator and interceptors. Declare an interface, mark a method parameter with [DuckTyped], and pass any type with matching members - no base class, no attributes, no implements clause required. The generator checks structural compatibility at compile time and routes calls through a generated adapter, producing an analyzer error if a type doesn't fit. daily.dev surfaces library releases like this for developers evaluating new C# patterns."}},{"@type":"Question","name":"Can I use anonymous types as stubs for unit tests in C#?","acceptedAnswer":{"@type":"Answer","text":"Yes, with the IfItQuacks library anonymous types satisfy any interface that structurally matches their members, so you can pass an anonymous object like new { Name = \"Steven\", Age = 90 } directly into a method expecting an interface such as IPerson, without writing a mock class. The library calls this pattern 'Instant Stubs'. Testing patterns like instant stubs are worth tracking for developers who write C# unit tests on daily.dev."}},{"@type":"Question","name":"Does duck typing in C# preserve object identity for Equals and GetHashCode?","acceptedAnswer":{"@type":"Answer","text":"Yes, in the IfItQuacks library, generated adapters forward Equals, GetHashCode, and ToString calls to the wrapped instance, so duck-typed values behave correctly in dictionaries and sets. The original instance can also be recovered explicitly using Duck.Unwrap, and two adapters wrapping the same instance are equal. daily.dev helps C# developers keep up with library behaviors like adapter identity semantics."}}]}
```

