Reputation: 23
I'm struggling getting my head around returning a List that has multiple elements (PHP background - I'd use arrays for this in PHP).
I have a large string that I'm parsing in a WHILE loop. I want to return a List with pairs of elements. I've tried something like this:
static public List<string> getdata(string bigfile)
{
var data = new List<string>[] { new List<string>(), new List<string>() }; // create list to hold data pairs
While (some stuff)
{
// add element pair to List<data>
data[0].Add(this); // add element to list - 'this' is declared and assigned (not shown)
data[1].Add(that); // add element to list - 'that' is declared and assigned (not shown)
}
return data???; // <<-- This is where I'm failing. I can, of course, return just one of the elements, like return data[0];, but I can't seem to get both elements (data[0] and data[1]) together.
} // end getdata
I've reviewed some answers, but I'm missing something. I've tried several things syntactically for the return value, but no luck. Any help would be greatly appreciated. I hate asking questions, but I've spent some time on this and I'm just not finding what I want.
Upvotes: 1
Views: 364
Reputation: 63065
try with
static public List<string>[] getdata(string bigfile)
{
....
}
Or
But if you need to return list of string array, then change the method as
static public List<string[]> getdata(string bigfile)
{
List<string[]> data= new List<string[]>();
While (some stuff)
{
data.Add(this);
data.Add(that);
}
return data;
}
Upvotes: 0
Reputation: 37770
I want to return a List with pairs of elements
If you want pairs, use pairs:
static public List<Tuple<string, string>> getdata(string bigfile)
{
var data = new List<Tuple<string, string>>(); // create list to hold data pairs
while (some stuff)
{
// add element pair
data.Add(Tuple.Create(a, b)); // 'a' is declared and assigned (not shown)
// 'b' is declared and assigned (not shown)
}
return data;
}
Upvotes: 0
Reputation: 222582
The problem there is you are returning a Collection of List so the return type is mismatching. Try this one,
var data = new List<string>();
while (some stuff)
{
data.Add("test0");
data.Add("test1");
}
return data;
Upvotes: 0
Reputation: 15893
Change method declaration to:
static public List<string>[] getdata(string bigfile)
Upvotes: 2