Home > .NET Development > >C# – Returning a Generic List

>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<T> GetAll<T>() where T : class

{
   List<T> result;
   if (typeof(T) == typeof(MyObject))
   {
     result = new List<T>
      {
          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<T>();
}
}

Advertisements
Categories: .NET Development Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: