Skip to main content

Home/ Coders/ Group items tagged Covariant

Rss Feed Group items tagged

Joel Bennett

The Performance of Arrays - Chris Burrows - 2 views

  • arrays of reference types are covariant in their element type, but not safely
  • where did that exception come from? It came from the runtime, which was in a unique position to know what the real type of the array object was, and the real type of the element. It needed to determine if the assignment was allowable and throw if not. Now if you think about this for a minute, you can see that it’s going to have to perform that check for every assignment to an array element
  • arrays are covariant only for reference types?
  • ...3 more annotations...
  • So if I want to have an array of reference types, but get the runtime to treat it like an array of value types, what have I got to do? Just wrap the reference type in a value type
  • So I got rid of that check, right? But I added the initialization of a value type. Did I win or lose? Let’s do some timing to figure it out.
  • when I build Debug binaries, the Reference<T> trick makes my example about three times SLOWER.
  •  
    Arrays are covariant only for reference types. If you use a struct wrapper to turn a reference type into a value type, the initialization of the value type takes less time than array assignment.
Jack Wong

Charlie Calvert's Community Blog : LINQ Farm: Covariance and Contravariance in C# 4.0 - 0 views

  •  
    null
1 - 2 of 2
Showing 20 items per page