Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Problem with generic interfaces and inheritance in C#

I have this definition of an abstract class:

public abstract class Game<T> : IGame<T> where T : IGameItem
{
    public Guid Id { get; set; } = Guid.NewGuid();

    protected List<T> GameItems { get; set; } = new();

    public IReadOnlyList<T> Items => GameItems.AsReadOnly();

    public void AddGameItem(T gameItem)
    {
        if(gameItem is null)
        {
            throw new ArgumentNullException(nameof(gameItem));
        }
        this.GameItems.Add(gameItem);
    }

    public void RemoveGameItem(T gameItem)
    {
        if (gameItem is null)
        {
            throw new ArgumentNullException(nameof(gameItem));
        }

        this.GameItems.Remove(gameItem);
    }
}

where IGame is (IItem only contains a property Id of type Guid):

public interface IGame<T> : IItem where T : IGameItem
{
    IReadOnlyList<T> Items { get; }

    void AddGameItem(T gameItem);

    void RemoveGameItem(T gameItem);
}

where IGameItem is inherits from IItem but it´s empty for the moment.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Then I have the definition of IWordGame which is:

public interface IWordGame : IGame<IWordGameItem>
{
   *** Some properties here ***
}

And IWordGameItem inherits from IGameItem and adds some other properties.

The problem comes when I create a class that derives from Game

public class WordGame : Game<WordGameItem>, IWordGame
{
}

The class keeps complaining that I haven´t implemented the properties and methods of the IGame interface… But those methods are implemented in the Game base class… What am I missing? Do I need to restructure the arquitecture? What´s the best way to do it? If I implement the methods this is how the code goes:

IReadOnlyList<IWordGameItem> IGame<IWordGameItem>.Items => throw new NotImplementedException();

public void AddGameItem(IWordGameItem gameItem)
{
    throw new NotImplementedException();
}

public void RemoveGameItem(IWordGameItem gameItem)
{
    throw new NotImplementedException();
}

However I don´t want to implement the methods there but in the base class. How can I achieve that?

>Solution :

You need to change WordGame to:

public class WordGame : Game<IWordGameItem>, IWordGame
{
}

The thing is that Game<WordGameItem> is not IGame<IWordGameItem> (imagine that it would me true, then with class WordGameItem1 : IWordGameItem you would be able to call Game<WordGameItem>.AddGameItem(new WordGameItem1()) which obviously is not type safe, this is explained in more detail for example in this answer).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading