views:

29

answers:

1

Is it a good idea to try and return a strongly typed List of custom objects from a webservice?

Any pitfalls I should know about?

[WebMethod]
public List<CustomSerializableObject> GetList()
{
    List<CustomSerializableObject> listToReturn = new List<CustomSerializableObject>();

    listToReturn.Add(new CustomSerializableObject());
    listToReturn.Add(new CustomSerializableObject());
    listToReturn.Add(new CustomSerializableObject());
    return listToReturn;
}
+1  A: 

I don't know of any specific pitfalls to speak of other than possible support for third-parties that would want to communicate to it. You would probably be better to return an array of objects by doing listToReturn.ToArray(). You can easily fill a new list on the client side if that is what you need.

Adam Gritt
What is the advantage of returning an Array of objects rather than a List?
Chris Ballance
As far as I know, a List will be converted to Array on return
asgerhallas
The XML serialized representation of `List<T>` is interchangeable with `T[]`
Rex M