views:

179

answers:

2

I learning about writing my own interfaces and came across this msdn article. Everything seems fine, except I cannot find anywhere what the < T > means or does. Can anyone help me out?

interface IEquatable<T>
{
    bool Equals(T obj);
}
+18  A: 

It means that it is a generic interface.

You could create an interface like this:

public interface IMyInterface<T>
{
    T TheThing {get; set;}
}

and you could implement it in various ways:

public class MyStringClass : IMyInterface<string>
{
    public string TheThing {get; set;}
}

and like this:

public class MyIntClass : IMyInterface<int>
{
    public int TheThing {get; set;}
}
klausbyskov
Thank you very much. Is there any reason that T is used?
m.edmondson
@eddy556: It's just a convention - T is used to represent "any type T" - however, sometimes you'll see other things (ie: Dictionary<TKey,TValue>, etc)
Reed Copsey
`T` is just a naming convention, because the generic argument name references a _T_ype. You could name it more or less whatever you want, but as it is a convention to name interfaces starting with an I it also a convention to name the generic type parameter(s) starting with T. In the above example it could just aswell have been named `TTheThing` or something along those lines.
klausbyskov
A: 

It is a parametric type means that you could reuse IEquatable for any type... at "runtime" (but not exactly), in place of T, you could use String, Animal, Dog ecc...

sh4d0000