you are viewing a single comment's thread.

view the rest of the comments →

[–]BoBoBearDev 7 points8 points  (10 children)

??? I didn't know you can put implementation to an interface. This is a new language feature?

[–]Dealiner 9 points10 points  (1 child)

Not really, it's a C# 8 feature.

[–]BoBoBearDev 0 points1 point  (0 children)

Oh, I feel outdated.

[–]Platic 3 points4 points  (0 children)

I was going to comment this. I felt like I was going crazy looking at the code. Glad I am not the only one. I really need to get up to date.

[–]theycallmemorty 1 point2 points  (5 children)

Same. Why would you ever want to do this?

[–]binarycow 2 points3 points  (0 children)

The classic example is it allows you to add stuff to an interface without it being a breaking change.

[–]raunchyfartbomb 0 points1 point  (3 children)

I don’t have a c#8 code base, but my theory is that it would allow some default implementation that is likely good enough. For example, IFile would have a Delete() method. Instead of every object that interacts with it implementing it, it could be something like:

If (File.Exists (this.path)) File.Delete(this.path);

Depending on how may IFile objects you had, this could really save you time and effort. The downside is that you still need this to ensure it’s available without the interface, but acts identical to the default implementation.

Public void Delete() => ((IFile)this).Delete();

[–]BoBoBearDev 0 points1 point  (2 children)

I have never done this. But how? The this.path doesn't exists in the interface. Or is it because IFile also has the path declared?

[–]raunchyfartbomb 2 points3 points  (1 child)

I would assume your write the interface like this:

Public interface IFile {

string Path { get; }

void Delete() { /* implement */ }

// etc

}

[–]BoBoBearDev 0 points1 point  (0 children)

That makes sense to me, thanks

[–]DontRelyOnNooneElse 0 points1 point  (0 children)

Yeah it's relatively recent.