0

I was researching at a fragment of someone else's code and something seemed curious to me.(just because I'm not familiar with the с# language)

there is an interface with the following method

   internal interface IPlayer
    {
        List<int> MakeMove(List<int> Gameboard, int Player);
    }

then a list is created in one of the classes and the interface is used there as a generic

    public List<IPlayer> AI_Players = new List<IPlayer> { };
. . .

    public void AIMove(int AI_Type)
    {
        Gameboard = AI_Players[AI_Type].MakeMove(Gameboard, Currentplayer);
    }

Can you please explain what is a list with interfaces as a generic?I understand what, for example, a list with an int generic is..but..what does this list store?

and what is [AI_Type] in this line?

Gameboard = AI_Players[AI_Type].MakeMove(Gameboard, Currentplayer);
5
  • 1
    "what is [AI_Type]" That's an indexer. AI_Type will be an integer giving the position in the list to access. Commented Mar 1, 2022 at 14:51
  • 1
    AI_Type is the parameter, and the [] is just the list indexer. Commented Mar 1, 2022 at 14:51
  • The AI_Players List is a list of object references but each object in that list supports the IPlayer interface, and is referred to by that interface. Commented Mar 1, 2022 at 14:59
  • @Wyck okay, thanks .and what would be the difference if the name of the class that implements this interface was written there as a generic? Commented Mar 1, 2022 at 15:01
  • I assume you would have something that looks like public List<T> AI_Players = new List<T> { }; But it can't be completely generic because AI_Player[AI_Type] is of type T but it must also support the .MakeMove method. So there would be a constraint that whatever interface T is would need to be derived from some other interface that supports .MakeMove Like an IMoveMaker or something then the generic would be constrained with where T: IMoveMaker and T could be anything that derives from IMoveMaker. Commented Mar 1, 2022 at 15:46

2 Answers 2

2

In short, this means that the list can only contain objects that implement this interface. So the individual elements of the list can be different concrete classes, but each of these classes must implement the interface.

Sign up to request clarification or add additional context in comments.

2 Comments

well, thank you. For example, if I have one class that implements this interface, then there would be no difference if I wrote as a generic the name of the class that implements this interface, right?
Design aspects aside, as long as it will remain just a single class, then it won't really make a difference.
2

AI_Players[AI_Type]

AI_Type would be an index. And it might be argued that a better name would be aiPlayerIndex, or that the list should be renamed to aiPlayerTypes;

what does this list store?

It stores references to objects, where the type of reference is IPlayer

Can you please explain what is a list with interfaces as a generic?

Interfaces is a kind of type, just like a int or a string is a type. A list can contain values of any type, int, string, IPlayer or any other type.

Note that the objects in the list might be of a different type than the reference. It is perfectly fine to have a list of IPlayer that contains both objects of AIPlayer and HumanPlayer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.