سؤال

I am getting a error in C# while trying to access a static method inside another class.

This is the class trying to access it:

class Problem7
{
    public void Solve()
    {
        GimmePrimes(new long[]{2, 3, 5, 7, 11, 13});
    }
}

And this is the static class where GimmePrimes is located:

public static class Extensions
{
    //....Other static methods that work//
    public static IEnumerable<long> GimmePrimes(this long[] firstPrimes)
        {
            return firstPrimes.Unfold(priorPrimes => priorPrimes.Last().OddNumbersGreaterThan().SkipWhile(candidate => priorPrimes.TakeWhile(prime => prime * prime <= candidate).Any(prime => candidate.IsDivisibleBy(prime))).First());
        }
}

Both classes are in the same namespace.

هل كانت مفيدة؟

المحلول

Extensions needs to be a static class.

public static class Extensions

Also, as it is an extension method, you're calling it wrong:

long[] array = new long[] {2, 3, 5, 7, 11, 13 };
IEnumerable<long> primes = array.GimmePrimes();

On a related side-note, when I have extension method classes, I create a separate one for each type I am extending so that I don't end up with a huge monolithic class.

public static class IntExtensionMethods
public static class StringExtensionMethods
public static class IEnumerableExtensionMethods

etc.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top