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);
[AI_Type]" That's an indexer.AI_Typewill be an integer giving the position in the list to access.AI_Typeis the parameter, and the[]is just the list indexer.AI_PlayersList is a list of object references but each object in that list supports theIPlayerinterface, and is referred to by that interface.public List<T> AI_Players = new List<T> { };But it can't be completely generic becauseAI_Player[AI_Type]is of typeTbut it must also support the.MakeMovemethod. So there would be a constraint that whatever interfaceTis would need to be derived from some other interface that supports.MakeMoveLike anIMoveMakeror something then the generic would be constrained withwhere T: IMoveMakerandTcould be anything that derives fromIMoveMaker.