-3

So I tried to generate the fibonacci algorithm in C# however, I everytime I run this code I am getting an error that states:

The type or namespace name `IList' could not be found. Are you missing a using directive or an assembly reference?

I am lost..where am I going wrong? Thank you...

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;
}
It'sNotALie.
  • 22,289
  • 12
  • 68
  • 103
user1883386
  • 99
  • 1
  • 4
  • 13
  • 3
    You forget `c#-2.0` tag. – Soner Gönül Jul 25 '13 at 11:56
  • add reference to `System.Collections.Generic` – Kamil Budziewski Jul 25 '13 at 11:57
  • 2
    BTW, you do not initialize `sequence`. – I4V Jul 25 '13 at 12:00
  • What are you trying to do in this line: sequence.Add(sequence[i], sequence[i+1]); – TheKingDave Jul 25 '13 at 12:00
  • I'd advise **against** returning an Interface (e.g. `IList`), unless your method actually returns different types of objects that all implement that interface. – Nolonar Jul 25 '13 at 12:01
  • I prefer using the Linq styled enumerator: [`public static IEnumerable Fibonacci() { long x = 0L; long y = 1L; long z; yield return x; yield return y; while (true) { z = x + y; yield return z; y = x; x = z; } }`](http://stackoverflow.com/questions/9263479/readable-unit-testing-of-a-yielded-sequence) – Steve B Jul 25 '13 at 12:03

2 Answers2

3

I bet you miss System.Collections.Generic, here is a correct version of your program

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();
    }
  }
}
Swift
  • 1,861
  • 14
  • 17
  • Houssem, i think you should post your eMail so that OP can ask his/her further homeworks directly to you ... :) – I4V Jul 25 '13 at 12:24
1

add using System.Collections.Generic and try it again

Anıl Canlı
  • 432
  • 9
  • 20
  • You may also hover your mouse over the red underlined `IList`, then click on the little box that appears or click on the word `IList` and press Ctrl + . (dot). A menu will appear and suggest possible ways to solve the problem. In this case, either by replacing `IList` with `System.Collections.Generic.IList` or by adding a `using System.Collections.Generic;`. – Nolonar Jul 25 '13 at 12:05