Extension Methods Must Be Defined in a Non-Generic Static Class

Extension methods must be defined in a non-generic static class

change

public class LinqHelper

to

public static class LinqHelper

Following points need to be considered when creating an extension method:

  1. The class which defines an extension method must be non-generic, static and non-nested
  2. Every extension method must be a static method
  3. The first parameter of the extension method should use the this keyword.

CS1106 Extension method must be defined in a non-generic static class

public void MoveTo(this Image target, double newY)

this on the first argument of a method definition indicates an extension method which, as the error message says, only makes sense on a non-generic static class. Your class isn't static.

This doesn't seem to be something that makes sense as an extension method, since it's acting on the instance in question, so remove the this.

Extension method must be defined in a non-generic static class

Add new static class and define your extension methods inside it. Check out MSDN documentation for Extension methods.

 namespace TrendsTwitterati 
{
public partial class Default: System.Web.UI.Page
{

}

public static class MyExtensions
{
public static IEnumerable < TSource > DistinctBy < TSource, TKey > (this IEnumerable < TSource > source, Func < TSource, TKey > keySelector)
{
HashSet < TKey > seenKeys = new HashSet < TKey > ();
foreach(TSource element in source)
{
if (seenKeys.Add(keySelector(element)))
{
yield
return element;
}
}
}
}
}

Cannot declare an extension method in a non-generic static class

After trying to understand your problem i think I understood it, first the error is wrong, the error on the compiler complains because an extension must be created in a non-generic static class, and the text you posted seems to say the contrary.

So, if the problem is that each T must have a different X, you need to store the class once created and reused then you can use a non-generic class with a dictionary:

public static class Extensions_S
{
static Dictionary<Type, X> generators = new Dictionary<Type, X>();

public static A generateA<T>(this T value)
{
X generator;

if(!generators.TryGetValue(typeof(T), out generator)
{
generator = new X(typeof(T));
generators.Add(typeof(T), generator);
}

return generator.from(value);
}
}

Extension method must be defined in non-generic static class

If you remove "this" from your parameters it should work.

public static IChromosome To<T>(this string text)

should be:

public static IChromosome To<T>(string text)


Related Topics



Leave a reply



Submit