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 :
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;
        }

    }
}

Hello, dieucay555.
Well,this is what MSDN says:

Generic Static Method

C# allows you to define static methods that use generic type parameters. However, when invoking such a static method, you need to provide the concrete type for the containing class at the call site, such as in this example:

public class MyClass<T>
{

public static T SomeMethod(T t)

{...}
}
int number = MyClass<int>.SomeMethod(3);

Static methods can define method-specific generic type parameters and constraints, similar to instance methods. When calling such methods, you need to provide the method-specific types at the call site, either explicitly:

public class MyClass<T>
{
public static T SomeMethod<X>(T t,X x)
{..}
}
int number = MyClass<int>.SomeMethod<string>(3,"AAA");

Or rely on type inference when possible:

int number = MyClass<int>.SomeMethod(3,"AAA");

Generic static methods are subjected to all constraints imposed on the generic type parameter they use at the class level. As with instance method, you can provide constraints for generic type parameters defined by the static method:

public class MyClass
{
public static T SomeMethod<T>(T t) where T : IComparable<T>
{...}
}

And here's the actual page: An Introduction to C# Generics

That one was hard to see!

using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace BookStore.Helper
{
   public static class ExtensionMethods
   {
      public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> source)
      {
         ObservableCollection<T> target = new ObservableCollection<T>();
         foreach (T item in source)
            target.Add(item);
         return target;
      }
   }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.