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:
- The class which defines an extension method must be
non-generic
,static
andnon-nested
- Every extension method must be a
static
method - 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
Accessing MVC's Model Property from JavaScript
Difference Between "\N" and Environment.Newline
Any Faster Way of Copying Arrays in C#
Parallel.Foreach VS Task.Run and Task.Whenall
How to Hide Only the Close (X) Button
Do C# Timers Elapse on a Separate Thread
How to Loop Through Items in a List Box and Then Remove Those Item
Httpclient - a Task Was Cancelled
How to Pass Values Across the Pages in ASP.NET Without Using Session
What's the Difference Between X = X++; VS X++;
How to Build a JSON Object to Send to an Ajax Webservice
How to Populate/Instantiate a C# Array with a Single Value
Accessing Password Protected Network Drives in Windows in C#
Ef Including Other Entities (Generic Repository Pattern)
Hosting External App in Wpf Window
Getting Path Relative to the Current Working Directory