-1

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?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
chamara
  • 12,649
  • 32
  • 134
  • 210

2 Answers2

4

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...

BrunoLM
  • 97,872
  • 84
  • 296
  • 452
0

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
SchautDollar
  • 348
  • 3
  • 13