Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Want to write a code to generate subset of fibonnaci. But i am getting this error GenerateSubset is not iterator interface type.

What I have tried:

public Task<IEnumerable<long>> GenerateSubset(int fromIndex, int toIndex)
    {
        int first =fromIndex;
        int second = fromIndex + 1;
        int third = 0;
        List<long> result = new List<long>();
        result.Add(first);
        result.Add(second);

        for (int i = fromIndex + 2; i <= toIndex - 1; i++)
        {
            third = first + second;
            result.Add(third);
            first = second;
            second = third;
        }
        yield return result;

    }
Posted
Updated 8-Jun-17 15:06pm
Comments
PIEBALDconsult 8-Jun-17 22:14pm    
You might not want the yield in there. Or you might want to move it.
Rajeshyadav12 8-Jun-17 22:27pm    
If i remove yield, then this error is coming 'Cannot implicitly convert type 'System.Collections.Generic.List<long>' to 'System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<long>>'

1 solution

Quote:
GenerateSubset is not iterator interface type.

I guess The compiler is unhappy that GenerateSubset return type is Task<IEnumerable<long>> when the type of return value is List<long>
 
Share this answer
 
Comments
Rajeshyadav12 8-Jun-17 21:42pm    
@ppolymorphe, Can you please provide me the solutions.I am not getting much idea.Appreciated if you provide me some solutions.
Richard Deeming 9-Jun-17 12:50pm    
Remove the yield, and change the method return type to IEnumerable<long>.

But your job isn't finished there. The code is not going to work if fromIndex != 1 - you need to review how the Fibonacci sequence[^] works.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900