Implementing Linq IENumerable<T> without new() constraint

  • Thread starter Thread starter Andrus
  • Start date Start date
A

Andrus

I noticed that Linq-SQL DataContext ExecuteQuery method does not have new()
constraint:

IEnumerable<TResult> ExecuteQuery( command, ... )

IEnumerable must return object so it must create this object in some way.

Any idea why and how ExecuteQuery<T> can be implemented witout new()
constraint ?

Andrus.
 
Andrus said:
I noticed that Linq-SQL DataContext ExecuteQuery method does not have new()
constraint:

IEnumerable<TResult> ExecuteQuery( command, ... )

IEnumerable must return object so it must create this object in some way.

Any idea why and how ExecuteQuery<T> can be implemented witout new()
constraint ?

Given that there's quite a bit of reflection involved (well, at least
initially - it's all compiled into lightweight functions, I believe)
one more bit to create instances isn't that much of a worry.

The new() constraint just lets the compiler use
Activator.CreateInstance, knowing that there'll be a parameterless
constructor avaialable (for classes - it uses initobj for value types).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top