1/5 "Ugly side of default interface methods" time. Interface with two methods, one with a default implementation. What will the following program print?
2/5 If you guessed 21, you're right, but don't pop the champagne yet. Let's change "class Adder" to "struct Adder" - what will it print now?
3/5 Hint: it's not 21 anymore. The important piece of knowledge to crack this is knowing that interfaces are reference types. To call a default interface method, we need a reference type. For structs, this means boxing.
4/5 Here's annotated version of AddAndPlusPlus.
5/5 To make this print 21 again, we need to provide an implementation of PlusPlus on the struct directly. Default interface methods don't play well with structs.
Bonus nightmare: what will the first and second call to Horror return? (Don't bother re-typing this - it's a mental exercise. We didn't implement this part for .NET Core 3.0 because I had better things to do - this is going to throw a NotSupportedException for now.)
Missing some Tweet in this thread? You can try to force a refresh.

Enjoying this thread?

Keep Current with Michal Strehovský

Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

Twitter may remove this content at anytime, convert it as a PDF, save and print for later use!

Try unrolling a thread yourself!

how to unroll video

1) Follow Thread Reader App on Twitter so you can easily mention us!

2) Go to a Twitter thread (series of Tweets by the same owner) and mention us with a keyword "unroll" @threadreaderapp unroll

You can practice here first or read more on our help page!

Follow Us on Twitter!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just three indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3.00/month or $30.00/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!