I’m struggling while trying to write generic methods to save and load data from a Sterling database.
This is my save method:
public static void SaveList<T>(List<T> listToSave)
{
foreach (T listItem in listToSave)
{
DatabaseInstance.Save(listItem);
}
}
I get squiggly red line under save and an error of “The Type T must be a reference type in order to use it as parameter T”
This is my load method:
public static List<T> LoadList<T>()
{
List<T> list = (from index in DatabaseInstance.Query<T, int>() select index.Key).ToList();
return list;
}
I get the same error.
Any ideas?
Cheers
Steve
UPDATE:
I added a where T : class as suggested and got an error:
The Type T must have a public parameterless constructor in order to use it as a parameter
Following instructions in the link provided by Bryan I added , new() on the end and all working now.
You need a generic type constraint on your definition: