+2  A: 

http://blogs.msdn.com/csharpfaq/archive/2004/03/12/88913.aspx

Roughly, much of the difference has to do with the fact that templates are resolved at compile-time, and generics are resolved at runtime.

McWafflestix
Actually although generics can be instantiated at runtime (through reflection, just like anything else), a lot is figured out at compile time. For example, the JIT produces a specific implementation of `List<int>` rather than using a common type-erased version equivalent to `List<object>` (this is one way it differs from Java).
Daniel Earwicker
@Earwicker: tell me about it. That's why you can't do generic promotion (i.e. concatenating a List<int> to a List<object>). The functionality exists in the CLI, but they chose not to implement it that way in C# (a mistake, IMO).
McWafflestix
A: 

This looks like a handy reference.

http://msdn.microsoft.com/en-us/library/c6cyy67b.aspx

Daniel Earwicker
+1  A: 

This blog entry from Eric Gunnerson covers this topic quite well.

The biggest immediate difference is that templates are a compile time feature whereas generics are a runtime feature.

Jeff Yates
+2  A: 

Extensive answer on Stack Overflow: http://stackoverflow.com/questions/31693/differences-in-generics

Richard Berg
Looks like this question is a duplicate. Thanks for the pointer.
bjarkef
+4  A: 

You can consider C++ templates to be an interpreted, functional programming language disguised as a generics system. If this doesn't scare you, it should :)

C# generics are very restricted; you can parameterize a class on a type or types, and use those types in methods. So, to take an example from MSDN, you could do:

public class Stack<T>
{
   T[] m_Items; 
   public void Push(T item)
   {...}
   public T Pop()
   {...}
}

And now you can declare a Stack or Stack and it'll store objects of that type, safely (ie, no worried about putting SomeOtherObject in by mistake).

Internally, the .NET runtime will specialize it into variants for fundamental types like int, and a variant for object types. This allows the representation for Stack to be much smaller than that of Stack, for example.

C++ templates allow a similar use:

template<typename T>
class Stack
{
    T *m_Items;
    public void Push(const T &item)
    {...}
    public T Pop()
    {...}
};

This looks similar at first glance, but there are a few important differences. First, instead of one variant for each fundamental type and one for all object types, there is one variant for each type it's instantiated against. That can be a lot of types!

The next major difference is (on most C++ compilers) it will be compiled in each translation unit it's used in. That can slow down compiles a lot.

Another interesting attribute to C++'s templates is they can by applied to things other than classes - and when they are, their arguments can be automatically detected. For example:

template<typename T>
T min(const T &a, const T &b) {
  return a > b ? b : a;
}

The type T will be automatically determined by the context the function is used in.

These attributes can be used to good ends, at the expense of your sanity. Because a C++ template is recompiled for each type it's used against, and the implementation of a template is always available to the compiler, C++ can do very aggressive inlining on templates. Add to that the automatic detection of template values in functions, and you can make anonymous pseudo-functions in C++, using boost::lambda. Thus, an expression like:

_1 + _2 + _3

Produces an object with a seriously scary type, which has an operator() which adds up its arguments.

There are plenty of other dark corners of the C++ template system - it's an extremely powerful tool, but can be painful to think about, and sometimes hard to use - particularly when it gives you a twenty-page long error message. The C# system is much simpler - less powerful, but easier to understand and harder to abuse.

bdonlan