>C# – Returning a Generic List

>Suppose I have a generic method that returns a generic list, code shows how you can do that.

public List GetAll() where T : class

{
   List result;
   if (typeof(T) == typeof(MyObject))
   {
     result = new List
      {
          new MyObject()
         {
            name = “ObjectName1”,
            id = 1
         } as T,
         new MyObject()

       {
          name = “ObjectName2”,
          id = 2
        } as T,
};
return result;
}
else //You can add other objects here if you’re using it in your Repository Test
{
return new List();
}
}