Generic Type Instantiation Again

C

Cool Guy

From <http://msdn2.microsoft.com/en-us/library/f4a6ta2h(en-US,VS.80).aspx>:

| As with the previous use of the Stack<T> class created with the Order
| type, another instance of the specialized Stack<T> class is created,
| and the pointers contained therein are set to reference an area of
| memory the size of a Customer type.

I don't quite get this. Where in memory do these changes (the pointers
getting set) take place?
 
N

Nicholas Paldino [.NET/C# MVP]

Cool Guy,

This is handled by the CLR. The CLR will create a separate type
definition for every distinct combination of value types used in a generic
type as type parameters. So, if you have Stack<int> and Stack<byte>, the
CLR will actually create two type definitions.

If you have a Stack<Customer> and Stack<Order> where Customer and Order
are reference types, then the CLR creates one type definition to use between
the two of them. The reason for this is that you are not dealing with the
actual memory that instances of the type take up, but rather, references,
which are all one, constant size.

Hope this helps.
 
C

Cool Guy

Nicholas Paldino said:
This is handled by the CLR. The CLR will create a separate type
definition for every distinct combination of value types used in a generic
type as type parameters. So, if you have Stack<int> and Stack<byte>, the
CLR will actually create two type definitions.

If you have a Stack<Customer> and Stack<Order> where Customer and Order
are reference types, then the CLR creates one type definition to use between
the two of them. The reason for this is that you are not dealing with the
actual memory that instances of the type take up, but rather, references,
which are all one, constant size.

That much, I understand, but the part of the article I quoted in the OP
threw me. I'm trying to figure out precisely which pointers are set*, in
order to understand what goes on under the hood.

* The quote was: "[...] the pointers contained therein are set to reference
an area of memory the size of a Customer type."
 
N

Nicholas Paldino [.NET/C# MVP]

Cool Guy,

Basically, the references are references to type Customer. However,
from the generic type's point of view, this doesn't matter. The only thing
the generic type cares about, if it is a reference type, is the fact that
because it is a reference type, the size requirements for fields and whatnot
are the same.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Cool Guy said:
Nicholas Paldino said:
This is handled by the CLR. The CLR will create a separate type
definition for every distinct combination of value types used in a
generic
type as type parameters. So, if you have Stack<int> and Stack<byte>, the
CLR will actually create two type definitions.

If you have a Stack<Customer> and Stack<Order> where Customer and
Order
are reference types, then the CLR creates one type definition to use
between
the two of them. The reason for this is that you are not dealing with
the
actual memory that instances of the type take up, but rather, references,
which are all one, constant size.

That much, I understand, but the part of the article I quoted in the OP
threw me. I'm trying to figure out precisely which pointers are set*, in
order to understand what goes on under the hood.

* The quote was: "[...] the pointers contained therein are set to
reference
an area of memory the size of a Customer type."
 
C

Cool Guy

Nicholas Paldino said:
Basically, the references are references to type Customer. However,
from the generic type's point of view, this doesn't matter. The only thing
the generic type cares about, if it is a reference type, is the fact that
because it is a reference type, the size requirements for fields and whatnot
are the same.

Yep, I understand that. :) I'm trying to work out precisely which
pointers are being spoken about in the following:

| [...]
| and the pointers contained therein are set to reference an area of
| memory the size of a Customer type.

The pointers contained in what?
 
C

Cool Guy

I wrote:

[snip]

Just to clarify -- I realise that it's not essential to know this in
general; I'm simply asking so that I can work what goes on under the hood
in this situation.
 
C

Cool Guy

I wrote:

[snip]

This is the kind of thing I'm talking about -- from
<http://www.artima.com/intv/generics2.html>:

| Anders Hejlsberg: [...] For all reference types
| we share the code, because they are representationally
| identical. It's just pointers.
|
| Bruce Eckel: And you just need to cast.
|
| Anders Hejlsberg: No, you don't actually. We can
| share the native image, but they actually have
| separate VTables. [...]

Now, *this* makes sense, but what I don't understand is where the

"the pointers contained therein are set to reference an area of memory
the size of a Customer type"

thing fits it. Any ideas?
 
M

Mattias Sjögren

Now, *this* makes sense, but what I don't understand is where the
"the pointers contained therein are set to reference an area of memory
the size of a Customer type"

thing fits it. Any ideas?

It sounds plain wrong (or just poorly worded) to me. This part is more
like a description of what happens when you create an instance of the
type parameter T. That doesn't happen when the Stack type is created.


Mattias
 
C

Cool Guy

Mattias Sjögren said:
It sounds plain wrong (or just poorly worded) to me. This part is more
like a description of what happens when you create an instance of the
type parameter T. That doesn't happen when the Stack type is created.

Thanks!
 

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

Top