C# Interfaces With Default Method Implementations #shorts

preview_player
Показать описание

Рекомендации по теме
Комментарии
Автор

I don't like it :/ default implementations in classes ok, but interfaces? why?

So, the main objective with the interfaces is that they are used as an abstract layer devoid of functionality, since there are abstract classes for that.

jose-sebastian-garcia
Автор

The real use case: you need to add some minor functionality (a new interface method) but you don't wanna your clients to change the code. You just replace dll with abstractions with a new one and the app doesn't break

antonmartyniuk
Автор

I used it. But granted: there are abstract classes that serve the same purpose.

nothingisreal
Автор

I’m not sure if it’s possible to use like this, but when you provide a public api library where you want to stay backwards compatible, but still want to rename a method on an interface you can make it call the old implementation and make the old implementation obsolete so that users can slowly switch to the new version

SelmirAljic
Автор

it is literally traits, so yea there are cases to use

nage
Автор

We learned interfaces after I already finished my project last year and they would have been soooo good for it

johnjacobjingleheimerschmi
Автор

I had used this in the past in an interesting case of a user repository interface with two implementations one of which was a distributed one using redis and required distributed locking. To avoid code duplication I moved the implementation to the interface and the redis implementation surrounded it with a distributed lock while the non redis one used the default from the interface.

kazepis
Автор

I use this when I have methods with overloads and default values to redirect to correct implemented method.

PatricSjoeoe
Автор

and how about multiple interfaces? isn't that why interfaces was interduced the first time?

omarattia-nj
Автор

the only use case I've stumbled upon with default implementation of interface members was not really in production, but could be interesting

it was one way to implement the state pattern, where the default behaviour of IState interface would be doing nothing, so when the object is not in the right state nothing happens on triggering it

it could be done with inheritance and virtual members, but default interface implementation allows you to not use override keyword, and I'm always happy to write less code :)

lettuceturnipthebeets
Автор

I used to declare the basic behavior of "IMapFrom" in AutoMapper.

johnnyjosep
Автор

Ask the Java guys they’ve had this for a while now

LiquaFoo
Автор

This is like the Family Guys meme "what the hell is this?" where the combination of abstract classes and an actual interface combines into this.

Seriously, I would not use it unless there is proven useful. In the mean time, Abstracts do the job quite well.

ronaldmarrou
Автор

Why us3 an interface and not an abstract class? What is the worth to use interface with implementation?

vittoriomorellini