CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Threaded View

  1. #1
    Join Date
    May 2001
    Location
    Silicon Valley
    Posts
    113

    Resolved What’s the best way of returning an array from a method?

    In plain C, I would do this:
    Code:
    void GetArray(
        mytype* pBuff   // [out] Pointer to the buffer in which array is returned to the calling code.
        int iBuffLen);  // [in]  Length of the buffer
    {
        for (i = 0; i < iBufflen; ++i) pBuff[i] = something;
    }
    Array is allocate once and never copied, unless I explicitly call for that.

    In C#, it's possible to return array from a method:
    Code:
    private byte [] GetArray()
    {
        byte[] iRet = {1,2,3,4,5};
        // ...
        return iRet;
    }
    Is this efficient? Is this good practice? Do C# methods always copy the local array when it's returned? Are there any other pitfalls?

    I found this article, which warns about pitfalls of using arrays with properties. At the same time, it doesn't talk about returning arrays from methods.

    Any suggestions references and insight are appreciated!

    Cheers,
    - Nick
    Last edited by kender_a; November 29th, 2010 at 02:09 AM. Reason: resolved

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured