Languages

C# / .NET - 'Enumerable' does not exist in the current context error

0 points
Asked by:
Kelly
394

When I try to use Enumerable.Repeat() method, I get the following error. How can I fix this?

Error:

'Enumerable' does not exist in the current context error

My code:

using System;

public class TestClass
{
    public static void Main()
    {
        string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        int length = 5;
        Random random = new Random();

        var result = Enumerable.Repeat(ALPHABET, length)
        .Select(s => s[random.Next(s.Length)]).ToArray();

        Console.WriteLine(result);
    }
}
1 answer
3 points
Answered by:
Kelly
394

You forgot to import System.Linq namespace.

Your code should look like:

using System;
using System.Linq;

public class TestClass
{
    public static void Main()
    {
        string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        int length = 5;
        Random random = new Random();

        var result = Enumerable.Repeat(ALPHABET, length)
        .Select(s => s[random.Next(s.Length)]).ToArray();

        Console.WriteLine(result);
    }
}

References

  1. Enumerable.Repeat<TResult>(TResult, Int32) Method (System.Linq) | Microsoft Docs
0 comments Add comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join