Jump to content

Cannot using generic type in extension method

- - - - -

  • Please log in to reply
1 reply to this topic

#1
dieucay555

dieucay555

    Newbie

  • Members
  • Pip
  • 2 posts
I want to write 1 extension method,it's purpose is convert 1 IEnumerable<T> to 1 ObservableCollection<T>, but i've error :
Error 5 The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?)
Here is my code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Data;

namespace BookStore.Helper
{
    public static class ExtensionMethods
    {
        public static ObservableCollection<T> ToObservableCollection(this IEnumerable<T> source)
        {
            ObservableCollection<T> target = new ObservableCollection<T>();
            foreach (T item in source)
                target.Add(item);
            return target;
        }

    }
}
Pls help me fix this bug!

#2
lespauled

lespauled

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 231 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash
Have you tried:

return new ObservableCollection<T>(source);




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users