Reputation: 348
I was trying to create a generic extension method using following code but it gives compile time error
public static class extentions<TSource, TResult>
{
public static IEnumerable<TSource> GetWholeHerichy(this IEnumerable<TSource> source, Func<TSource, TResult> KeySelector, Func<TSource, TResult> FKeySelector, TResult KeyElementid)
{
var k = source.Where(a => EqualityComparer<TResult>.Default.Equals(KeyElementid, KeySelector(a))).ToList();
while(true)
{
var nextLevel = source.Where(a => k.Select(b => KeySelector(b)).Contains(FKeySelector(a)) && ! k.Select(b => KeySelector(b)).Contains(KeySelector(a)));
if (nextLevel == null || nextLevel.Count() < 1)
break;
k.AddRange(nextLevel);
}
return k.AsEnumerable();
}
}
Error is
Error 1 Extension method must be defined in a non-generic static class
if i removes <TSource, TResult>
from public static class extentions<TSource, TResult>
to make class non-generic then TSource
and TResult
became undefined in whole class
Upvotes: 0
Views: 63
Reputation: 111820
You must "move" the generic types in the method signature:
public static IEnumerable<TSource> GetWholeHerichy<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> KeySelector, Func<TSource, TResult> FKeySelector, TResult KeyElementid)
exactly like the various Enumerable.*
methods.
Upvotes: 2