0
  1. Using instance

if (new T() is ISoftDelete)

  1. Using type(IsAssignableFrom)

if (typeof(ISoftDelete).IsAssignableFrom(typeof(T)))

Is there any difference between these two line regarding efficiency, and which one would be better to use. T is Entity, and it has default parameterless constructor, so there is nothing in constructor. I know option 1. creates new instance, and options 2. looks like it is using reflection.

PS This is not the same as linked question because there 'obj' was existing object and here I have only generic class T, not instance so the answer might not be the same. Here options are calling empty constructor vs calling typeof() 2 times?

borisdj
  • 2,201
  • 3
  • 24
  • 31
  • In addition to the duplicate the first version assumes `T` has a parameterless constructor. Furthermore it unneccessarily creates an instance of that. – MakePeaceGreatAgain Jul 19 '17 at 12:19
  • If performance is an issue then try both and see which one runs faster. I would be inclined to think the second is better before you even consider that the first might do any number of things in the constructor, some of which might be very time or resource consuming. – Chris Jul 19 '17 at 12:22
  • I can add T is Entity, and it has default parameterless constructor, so there is nothing in constructor. – borisdj Jul 19 '17 at 12:35

0 Answers0