Recently I came across the below code.
public interface IBlog<T>
{
void Add(T blog);
IEnumerable<T> GetAll();
T GetRecord(int id);
void Delete(int id);
}
What is T here? What is the purpose of using it?
Recently I came across the below code.
public interface IBlog<T>
{
void Add(T blog);
IEnumerable<T> GetAll();
T GetRecord(int id);
void Delete(int id);
}
What is T here? What is the purpose of using it?
A simple example, you can have a method
T GetDefault<T>()
{
return default(T);
}
and call
int zero = GetDefault<int>();
T in the method will be the type of an int.
In c# you have List<int> or List<string>, for example, this was implemented using generics, read more...
What you are wondering about are Generics. Generics provide a nice dynamic way doing stuff. You may or may not be aware of this already, but List and Dictionary use generics.
List<Foo> foos = new List<Foo>(); //Means everything within that list will be of Foo type
List<Bar> bars= new List<Bar>(); //Again, means everything within that list will be of Bar type