I am a beginner in C-sharp, and I recently encountered a problem. I don"t understand why I got this error
index was out of range.Must be non-negative and less than size of the collection
code as follows
class Program
{
delegate bool Function(int a , int b);
static Function b = delegate(int a ,int b){return a>=b;};
static Function a = delegate(int a ,int b){return a<=b;};
static List<int> Lis(List<int> nums , Function function)
{
var list = new List<int>();
int maxOrMin = list[0];
foreach(int num in nums)
{
if(function(num,maxOrMin))
{
maxOrMin = num;
}
}
list.Add(maxOrMin);
return list;
}
static void Main()
{
var list = Lis(new List<int> (){1,2,3,4,5,6} , b);
foreach(var lis in list)
{
Console.WriteLine(lis);
}
}
}