© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
22 replies
TYoemtais.z

Polymorphism in C# makes no sense

Let's assume such a case:
public interface ICar
{
    string Name { get; }
    IEngine Engine { get; }
}

public interface IEngine
{
    string Name { get; }
}

public class Engine: IEngine
{
    public string Name { get; }
    public string EngineClassMethod()
    {
        return "Engine class";
    }
}

public class Car : ICar
{
    public string Name { get; }
    public Engine Engine { get; }
}
public interface ICar
{
    string Name { get; }
    IEngine Engine { get; }
}

public interface IEngine
{
    string Name { get; }
}

public class Engine: IEngine
{
    public string Name { get; }
    public string EngineClassMethod()
    {
        return "Engine class";
    }
}

public class Car : ICar
{
    public string Name { get; }
    public Engine Engine { get; }
}

And for the Car class I have an error
Error    CS0738    'Car' does not implement interface member 'ICar.Engine'. 'Car.Engine' cannot implement 'ICar.Engine' because it does not have the matching return type of 'IEngine'
Error    CS0738    'Car' does not implement interface member 'ICar.Engine'. 'Car.Engine' cannot implement 'ICar.Engine' because it does not have the matching return type of 'IEngine'

I could use IEngine instead of Engine in Car, which would fix the error. But then I can't use the methods of the Engine class.
I completely don't understand why I can't use Engine since this class implements IEngine.
I can't have different classes implementing ICar and having inside different classes implementing IEngine, they all have to be on the base IEngine.
This is terribly frustrating.
Instead of using interfaces in such cases, I have to get rid of them.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ this error makes no sense
C#CC# / help
17mo ago
Polymorphism
C#CC# / help
2y ago
❔ Polymorphism
C#CC# / help
3y ago
❔ Polymorphism
C#CC# / help
4y ago