What would be the difference between these two seemingly similar declarations?
When would you choose one syntax over another?
Is there any specific reason to choose one over the other?
Any performance penalties incurred for either case?
public void Import<T>(
Func<IEnumerable<T>> getFiles, Action<T> import)
where T : IFileInfo
{
// Import files retrieved through "getFiles"
}
public void Import(
Func<IEnumerable<IFileInfo>> getFiles, Action<IFileInfo> import)
{
// Import files retrieved through "getFiles"
}
The difference is that the first would allow you to pass in something which used a more concrete type implementing
IFileInfo. For example:(Where
SpecialPropertyis a property which exists onSpecialFileInfobut notIFileInfo.) You couldn’t do this with the latter form.There is a very slight execution-time penalty for generic methods and types – it will be JITted once for different each value type
Ttype argument (don’t forget a value type could implementIFileInfo) and once for all reference type (i.e. once it’s been JITted or one reference type, it won’t need to be JITted again). This will almost certainly be negligible in your real application though.