Return ArrayList from Method - How?

M

mp

Hi,
Is possible to return ArrayList or Array from Method and how?

Where I can find some example?

Thanks,
Mirko
 
D

Dmitriy Lapshin [C# / .NET MVP]

Hi,

Here's an example:


using System.Collections;

public class ArrayListRetriever
{
public ArrayList GoRetrieve()
{
ArrayList al = new ArrayList();

// Now populate al with some data...

return al;
}
}
 
J

Jon Skeet [C# MVP]

mp said:
Is possible to return ArrayList or Array from Method and how?

Where I can find some example?

Just declare it as the return type, e.g.

ArrayList Foo()
{
ArrayList ret = new ArrayList();
ret.Add("Hello");
ret.Add("There");
return ret;
}
 
G

Guest

private ArrayList returnArrayList()
{
ArrayList theArrayListToReturn = new ArrayList ();

// Do your work

return theArrayListToReturn;
}
 
A

Andreas Håkansson

Hello,

To return an ArrayList from a method, do like has been suggested in
previous replies. If you want to return an array then do like the follwing
(for an array of ints for example)

public int[] Foo
{
int[] value = new int[10];
int[0] = 1;
int[1] = 2;
return values;
}

HTH,

//Andreas
 
N

Not important who I am

In the same way as you would return any other value from a method:

ArrayList someMember()
{
ArrayList al = new ArrayList();

return al;
}
 
C

C# Learner

mp said:
Hi,
Is possible to return ArrayList or Array from Method and how?

Where I can find some example?

Just to add...

When you return an array or ArrayList instance from a method, you are
simply returning a *reference*.

Here's an example of using a /returned/ reference to an ArrayList instance:

class Test
{
static void Main()
{
ArrayList list = GetArrayList();
list.Add("number two");
// 'list' now contains two items
Console.WriteLine(list[0] as string);
Console.WriteLine(list[1] as string);
Console.Read();
}

static ArrayList GetArrayList()
{
ArrayList result = new ArrayList();
result.Add("number one");
return result;
}
}
 

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