-
November 27th, 2010, 10:43 PM
#1
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|