I have the class PlayersCollection and I want to interface it in IWorldCollection. The probleme is about writing the declaration in the interface which cause me this error :
Assets/Scripts/Arcane/api/Collections/ItemsCollection.cs(17,22): error CS0425: The constraints for type parameter `T' of method `Arcane.api.ItemsCollection.Get<T>(int) must match the constraints for type parameter `T' of interface method `Arcane.api.IWorldCollection.Get<T>(int)'. Consider using an explicit interface implementation instead Here is my class and my interface. How to write a generic method implementation with a class constraint ?
public class PlayersCollection : IWorldCollection { public Dictionary<Type, object> Collection; public PlayersCollection() { Collection = new Dictionary<Type, object>(); } public T Get<T>(int id) where T: PlayerModel { var t = typeof(T); if (!Collection.ContainsKey(t)) return null; var dict = Collection[t] as Dictionary<int, T>; if (!dict.ContainsKey(id)) return null; return (T)dict[id]; } } } public interface IWorldCollection { T Get<T>(int id) where T : class;// Work when I change "class" to "PlayerModel". } Big thanks for your help :)