Returning Arrays from Methods

  • Thread starter Thread starter Mortimer Schnurd
  • Start date Start date
M

Mortimer Schnurd

I'm not quite sure of how to do this and I haven't been able to find
much about it. I'm hoping someone here can help me out.

In a nutshell,: I would like to use a foreach loop to iterate through
an Array which is returned by a method,

foreach ( string groupName in MySplitterFunction(stringToSplit) )

Now, how do I write the method so it returns an Array? Can any one
provide a simple example?

Thanks in advance,
--
Regards,
John Wood a.k.a Mortimer Schnurd
(e-mail address removed)

(to reply: remove "dropthistag")
 
private string[] isGuilty()
{
string[] result = new string[]{"Of", "course", "Peterson", "is",
"guilty"};
return result;
}

ShaneB
 
Mortimer Schnurd said:
I'm not quite sure of how to do this and I haven't been able to find
much about it. I'm hoping someone here can help me out.

In a nutshell,: I would like to use a foreach loop to iterate through
an Array which is returned by a method,

foreach ( string groupName in MySplitterFunction(stringToSplit) )

Now, how do I write the method so it returns an Array? Can any one
provide a simple example?

Sure:

using System;

class Test
{
static void Main()
{
foreach (string name in GetNames())
{
Console.WriteLine (name);
}
}

static string[] GetNames()
{
string[] ret = {"Matthew", "Mark", "Luke", "John"};
return ret;
}
}
 
John,

Here is an example of a function:

public string[] getStrings(){
string[] strings = new string[]{"hey", "world!"};

return strings;
}

You just have to make sure that you are declaring the return type as an
array.

Thanks,
Ian Suttle
http://www.IanSuttle.com
 
Thanks for the all the replies. They definitely hit the mark and
solved my problem. I have to make special mention of ShaneB's solution
though. Right on, ShaneB!
--
Regards,
John Wood a.k.a Mortimer Schnurd
(e-mail address removed)

(to reply: remove "dropthistag")
 
ShaneB said:
private string[] isGuilty()
{
string[] result = new string[]{"Of", "course", "Peterson", "is",
"guilty"};
return result;
}

Please keep your political opinions to yourself.
 
What does that have to do with politics? Anyway, it was merely a joke...I
just happened to be watching CourtTV when I replied.

Lighten up.

ShaneB

Daniel O'Connell said:
ShaneB said:
private string[] isGuilty()
{
string[] result = new string[]{"Of", "course", "Peterson", "is",
"guilty"};
return result;
}

Please keep your political opinions to yourself.
 
Back
Top