I am getting compile error error "The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?
Below is my code:
static class ExtensionMethods
{
public static Collection<T> ToCollection(this IEnumerable<T> source)
{
Collection<T> sourceCollection = new Collection<T>();
foreach (T currentSourceInstance in source)
{
sourceCollection.Add(currentSourceInstance);
}
return sourceCollection;
}
}
Change it to this:
public static class ExtensionMethods
{
public static Collection<T> ToCollection<T>(this IEnumerable<T> source)
{
Collection<T> sourceCollection = new Collection<T>();
foreach (T currentSourceInstance in source)
{
sourceCollection.Add(currentSourceInstance);
}
return sourceCollection;
}
}
Notice the ToCollection<T>
, otherwise the compiler doesn't understand where this T
is coming from.
You can call it like this (where Thing
is your custom type in this example):
var items = new List<Thing>();
var collection = items.ToCollection<Thing>();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With