A question about interfaces that run on Java but not in C#

Viewed 70

I have a service layer structure works on Java without any error. But when I try it on C# it's not working as I expected. I have a common service interface IService which has a method Update that returns another interface IUpdateResult. And I have a custom service interface IAService like below.

public interface IService 
{
    IUpdateResult Update();
}

public interface IAService : IService 
{     

} 

And finally I have a UpdateResult class which implements IUpdateResult.

public class UpdateResult : IUpdateResult
{
}

I am implementing custom service class like below and it does not allow me to do this.

public class AService : IAService
{
    public UpdateResult Update()
    {
        // bla bla
    }
}

As I mentioned it's working on Java the way I want. Why does C# give me error?

1 Answers

As I mentioned it's working on Java the way I want. Why does C# give me error?

Because Java and C# are different languages, designed by different teams of people. :)

Don't worry, as the comments said, this feature is planned for C# 9.

For now, you can use an explicit interface implementation:

public class AService : IAService
{
    public UpdateResult Update()
    {
        return null;
    }
    
    IUpdateResult IService.Update() { return Update(); }
}
Related