471,337 Members | 931 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

Generic Type Instantiation Again

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?
Dec 7 '05 #1
8 1530
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Cool Guy" <co*****@abc.xyz> wrote in message
news:1r***************@cool.guy.abc.xyz...
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?

Dec 7 '05 #2
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote:
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."
Dec 7 '05 #3
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]
- mv*@spam.guard.caspershouse.com

"Cool Guy" <co*****@abc.xyz> wrote in message
news:db***************@cool.guy.abc.xyz...
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote:
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."

Dec 7 '05 #4
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote:
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?
Dec 7 '05 #5
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.
Dec 7 '05 #6
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?
Dec 7 '05 #7
>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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Dec 7 '05 #8
Mattias Sjögren <ma********************@mvps.org> wrote:
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.


Thanks!
Dec 8 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Joanna Carter \(TeamB\) | last post: by
4 posts views Thread by Cool Guy | last post: by
6 posts views Thread by Dan Holmes | last post: by
3 posts views Thread by Peter K | last post: by
11 posts views Thread by paul.gibson | last post: by
11 posts views Thread by Scott Stark | last post: by
8 posts views Thread by Advait Mohan Raut | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.