Rea*_*apo 0 c# linq visual-studio-express
我在尝试访问另一个类中的静态方法时遇到C#错误.
这是尝试访问它的类:
class Problem7
{
public void Solve()
{
GimmePrimes(new long[]{2, 3, 5, 7, 11, 13});
}
}
Run Code Online (Sandbox Code Playgroud)
这是GimmePrimes所在的静态类:
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());
}
}
Run Code Online (Sandbox Code Playgroud)
两个类都在同一名称空间中.
Extensions 需要是一个静态类.
public static class Extensions
Run Code Online (Sandbox Code Playgroud)
另外,因为它是一种扩展方法,所以你说错了:
long[] array = new long[] {2, 3, 5, 7, 11, 13 };
IEnumerable<long> primes = array.GimmePrimes();
Run Code Online (Sandbox Code Playgroud)
在相关的附注中,当我有扩展方法类时,我为每个扩展的类型创建一个单独的类,这样我就不会得到一个巨大的单片类.
public static class IntExtensionMethods
public static class StringExtensionMethods
public static class IEnumerableExtensionMethods
Run Code Online (Sandbox Code Playgroud)
等等