Return an Array

G

Guest

Hello, Newsgroupians:

I have a simple question. I have a function that I want to return an array.
I want the array to be other objects that were instantiated in the function
itself. What's the correct way to return the array? Here's what I would
like to happen...

public object[] MyFunc()
{
object obj1 = SomeOtherFunc1();
object obj2 = SomeOtherFunc2();
object obj3 = SomeOtherFunc3();

return object[3] {obj1, obj2, obj3}; // Of course this is an error
}

But, of course, this doesn't work. I see that creating a new instance of
the array and then assigning the array to object may be inefficient...

object obj1 = SomeOtherFunc1();
object obj2 = SomeOtherFunc2();
object obj3 = SomeOtherFunc3();

object retVal[] = {obj1, obj2, obj3};
return retVal;

Although it may seem inefficient, is this the proper way to return an array
of objects? Is there a better way? Thank you all.


Trecius
 
N

Nicholas Paldino [.NET/C# MVP]

Trecius,

You are forgetting to use "new", like so:

public object[] MyFunc()
{
object obj1 = SomeOtherFunc1();
object obj2 = SomeOtherFunc2();
object obj3 = SomeOtherFunc3();

// Emphasise on NEW mine.
return NEW object[3] {obj1, obj2, obj3};
}
 
G

Guest

Thank you, Mr. Paldino. It worked perfectly!

Trecius

Nicholas Paldino said:
Trecius,

You are forgetting to use "new", like so:

public object[] MyFunc()
{
object obj1 = SomeOtherFunc1();
object obj2 = SomeOtherFunc2();
object obj3 = SomeOtherFunc3();

// Emphasise on NEW mine.
return NEW object[3] {obj1, obj2, obj3};
}


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Trecius said:
Hello, Newsgroupians:

I have a simple question. I have a function that I want to return an
array.
I want the array to be other objects that were instantiated in the
function
itself. What's the correct way to return the array? Here's what I would
like to happen...

public object[] MyFunc()
{
object obj1 = SomeOtherFunc1();
object obj2 = SomeOtherFunc2();
object obj3 = SomeOtherFunc3();

return object[3] {obj1, obj2, obj3}; // Of course this is an error
}

But, of course, this doesn't work. I see that creating a new instance of
the array and then assigning the array to object may be inefficient...

object obj1 = SomeOtherFunc1();
object obj2 = SomeOtherFunc2();
object obj3 = SomeOtherFunc3();

object retVal[] = {obj1, obj2, obj3};
return retVal;

Although it may seem inefficient, is this the proper way to return an
array
of objects? Is there a better way? Thank you all.


Trecius
 
H

Hilton

Use:

return new object[]{obj0, obj1, obj2};

or alternatively use "out" parameters if you always have a fixed number of
'output' values - this technique does not create the additional array and
might be faster in inner loop situations.

Hilton
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top