locked
Return a list via a parameter or a return value RRS feed

  • Question

  • I'd know how to do this in C++, but I have no idea what is best in C#. I have a list of polylines (each a list of points):

    List<CPolyLine> PolyLineList ;

    and I have a function which will create this list, consisting of maybe thousands of CPolylines.

    Should I use an out parameter in my (void)function?

    void CreatePolylineList (out PolylineList PlineList, int a, double b) 
    Or should I simply return the list?
    PolylineList CreatePolylineList (int a, double b)


    http://www.ransen.com Cad and Graphics software

    Sunday, August 14, 2016 12:44 PM

Answers

  • Just return a List<T> out of the method making the method a Function();

    public List<someobj> GetObjects(int a, double b)

    Note: because a collection, a List<T>,  is being returned, you can use Linq on the method to further filter results of the retuned collection.

    public List<someobj> GetObjects(int a, double b).Where(a => a.Name == "Owen").FirstOrDefault();

     

    • Marked as answer by Owen Ransen Sunday, August 14, 2016 4:49 PM
    Sunday, August 14, 2016 3:17 PM

All replies

  • If each Polyline is a List of points, the proper type would be:
    List<Point> (note that there is more then 1 point in the .NET Framework; Inlcuding a few with non-integer values).

    CreatePolylineList:
    This function seems misnamed entirely. It will not create list, only a single instance. Out serves no purpose here as lists are resizing internally. You should propably just get rid of that function and add a element to the list like this:

    List<Point> Polyline = new List<Point>(1000);
    //This code in a loop
    Polyline.Add(new Point(a, b);

    Giving the list the expected size during initilisation avoids it having to resize pointlessly. But if the effective size varies a lot, you might want to trim it after filling it.

    If you want a List of Polylines, things get even more complicated. Basically you have to make a jagged list:

    List<List<Point>> PolylineList;

    For overiews sake, you might want to use a alias or custom class for the Polylines as a whole:

    public class Polyline : List<Point>{
      //No code here
    }
    
    //Below:
    List<Polyline> PlList;
    There is no performance gain from using out over return values. If anything the opposite might be true. Unlike ref it also forces you to assign/override that variable, so it is not even a semantic equivalent. Also consider this article: Which is faster? | Fabulous Adventures In Coding

    Sunday, August 14, 2016 2:54 PM
  • Just return a List<T> out of the method making the method a Function();

    public List<someobj> GetObjects(int a, double b)

    Note: because a collection, a List<T>,  is being returned, you can use Linq on the method to further filter results of the retuned collection.

    public List<someobj> GetObjects(int a, double b).Where(a => a.Name == "Owen").FirstOrDefault();

     

    • Marked as answer by Owen Ransen Sunday, August 14, 2016 4:49 PM
    Sunday, August 14, 2016 3:17 PM
  • >> There is no performance gain from using out over return values.

    Ok, thanks for that. It does seem clearer when programming as well.

    (The name is correct, these are not .NET points but DXF points in 3D with thicknesses as well as well as layers. A Polyline in DXF is a very specific entity which cannot be summed up using a single .NET class. The function creates and initialises the list with values depending on the parameters, simpliied in my example.)


    http://www.ransen.com Cad and Graphics software

    Sunday, August 14, 2016 4:49 PM
  • Thanks, I'll do as you suggest...

    http://www.ransen.com Cad and Graphics software

    Sunday, August 14, 2016 4:49 PM