That's an interesting hypothetical world. In the real world, if I do this:
static void Main(string[] args)
{
List<int> items = new List<int>(new int[] { 1, 2, 3, 4, 5, 1, 2, 3, 4 });
List<int> nextIndices = new List<int>();
try
{
for (int i = 0; i < items.Count() - 1; i++)
{
nextIndices.Add(FindIndexAfter(items, i, toMatch => toMatch == items\[i\]));
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
for (int j = 0; j < nextIndices.Count(); j++)
{
Console.WriteLine(nextIndices\[j\]);
}
Console.WriteLine("Program complete...");
Console.ReadKey();
}
static int FindIndexAfter<T>(List<T> items, int startIndex, Func<T, bool> predicate)
{
if (startIndex < 0)
throw new ArgumentOutOfRangeException("startIndex",
"Start index cannot be negative.");
if (startIndex >= items.Count())
throw new ArgumentOutOfRangeException("startIndex",
"Start index must be less than the number of items");
startIndex++;
while (startIndex < items.Count())
{
if (predicate(items[startIndex]))
return startIndex;
startIndex++;
}
return -1;
}
I print:
5
6
7
8
-1
-1
-1
-1
Program Complete
Passing startIndex
as ref
(which is close to what you are suggesting as the default), I get this:
1
3
5
7
Program Complete
Since structs are just immutable classes, you now also return by reference, which would mean that you would be filling the list with references to variable i
instead of the values returned by the function. I'm not going to try to simulate that sort of return, but I'm pretty sure you would end up with a list filled with items.Count()
when you were done. So, now you either have to invent some "return by value" notation or do something like return new int(startIndex);
Along the same line, you also now only have assignment by reference. Which means that
int i = 1;
int j = i;
j++;
Console.WriteLine(i);
now prints 2 instead of 1. Again, you would need some new syntax to specify "assign by value" instead of by reference. All in all, I just don't think "structs are just immutable classes" (and the default pass-by-reference that comes from it) would actuall