What is the difference between the parameter and T common with the restriction? WITH#

I really don't understand what is the difference between:

private void Send<T>(T packet) where T : IPacket

and

private void Send(IPacket packet)

since there is a general restriction, isn’t it the same? If not, what is the difference between the dots and what are the advantages of using a common with a restriction compared to a single character?

Thank!

+4
source share
1 answer

What is the difference in values ​​here and what are the advantages of using a common with a restriction compared to a simple one?

Using generics, you can specify several restrictions:

private void Send<T>(T packet) where T : IPacket, IFoo {
}
...

private void Send<T>(T packet) where T : IPacket, new() {
  var t1 = new T();
  var t2 = default(T);
}

, , , .

+3

All Articles