所以我尝试在C#中生成fibonacci算法但是,我每次运行此代码时都会收到错误消息:
找不到类型或命名空间名称`IList'。你错过了吗 using指令或汇编引用?
我迷路了......我哪里错了?谢谢......
public IList<int> GenerateFibonacci(int toIndex)
{
IList<int> sequence;
sequence.Add(0);
sequence.Add(1);
for (int i=0; i<toIndex; i++)
{
sequence.Add(sequence[i], sequence[i+1]);
}
return sequence;
}
答案 0 :(得分:3)
我打赌你错过了System.Collections.Generic
,这是你程序的正确版本
using System;
using System.Collections.Generic;
namespace ConsoleApplication2
{
public class Program
{
public IList<int> GenerateFibonacci(int toIndex)
{
IList<int> sequence = new List<int>();
sequence.Add(0);
sequence.Add(1);
for (int i = 0; i < toIndex; i++)
{
sequence.Add(sequence[i]+ sequence[i + 1]);
}
return sequence;
}
static void Main()
{
var s = GenerateFibonacci(10);
Console.ReadKey();
}
}
}
答案 1 :(得分:1)
使用System.Collections.Generic添加并再次尝试