Confused about interface default implementations [Answered]

Trying out default implementations for methods in the interfaces for the first time. In the interface there's a public method that if I define in a class, that class implementation is used. However, the second method is the same concept but when called, the default implementation from the interface is called instead. I found out it has to do with the method being internal instead of public but why does it work like that?
9 Replies
TheBoxyBear
TheBoxyBearOP3y ago
interface IFoo
{
public void A() {}
internal void B() {}
}

class Foo : IFoo
{
public void A() {} // Always runs if the runtime type is Foo
internal void B() {} // Doesn't run unless made public
}
interface IFoo
{
public void A() {}
internal void B() {}
}

class Foo : IFoo
{
public void A() {} // Always runs if the runtime type is Foo
internal void B() {} // Doesn't run unless made public
}
Fixed it by explicitly implementing the internal method but still curious about the logic behind it compared to public methods
333fred
333fred3y ago
Because the CLR does not look at internal methods when considering interface implementations
Accord
Accord3y ago
✅ This post has been marked as answered!
TheBoxyBear
TheBoxyBearOP3y ago
Kinda makes sense, interfaces are meant to be a promise about what you can do with a type. Having it internal breaks the promise
333fred
333fred3y ago
Now that you've bumped the thread after marking it closed, you need to manually archive it @TheBoxyBear
TheBoxyBear
TheBoxyBearOP3y ago
NotLikeThis no idea how
333fred
333fred3y ago
Right click
TheBoxyBear
TheBoxyBearOP3y ago
Nothing about archiving in the context menu
Accord
Accord3y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server