Generic Type Instantiation

C

Cool Guy

I don't understand the third paragraph under the heading 'Generic type
instantiations' on
<http://msdn.microsoft.com/vcsharp/2005/overview/language/generics/>:

| The .NET Common Language Runtime creates a specialized copy of the
| native code for each generic type instantiation with a value type,
| but shares a single copy of the native code for all reference
| types (since, at the native code level, references are just
| pointers with the same representation).

Would someone mind putting that into different words?
 
R

Richard Blewett [DevelopMentor]

Cool Guy said:
I don't understand the third paragraph under the heading 'Generic type
instantiations' on
<http://msdn.microsoft.com/vcsharp/2005/overview/language/generics/>:

| The .NET Common Language Runtime creates a specialized copy of the
| native code for each generic type instantiation with a value type,
| but shares a single copy of the native code for all reference
| types (since, at the native code level, references are just
| pointers with the same representation).

Would someone mind putting that into different words?

There is a single copy of the IL for a generic type. However, when the JIT
compiler has to emit the native code for any particular spcialization of the
generic type there are issues. For reference types its easy, the native code
is just dereferencing a pointer to the type used to specialise the generic
so they can all share the same native code.

However for value types things are more complex. Variables of the
specializing type will be different sizes and so a generic instance will
occupy different amounts of space. What about floating point and integer
arithmatic, the machine instructions will be different for these. So for
value types the JIT compiler emits a different copy of the native code for
each specialization.

class Foo<T>
{
}

Foo<string> and Foo<SqlConnection> can share the same native code

Foo<int>, Foo<long> and Foo<float> each have their own copy

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
 
C

Cool Guy

"Richard Blewett [DevelopMentor]" <richard at nospam dotnetconsult dot co
dot said:
[...]

class Foo<T>
{
}

Foo<string> and Foo<SqlConnection> can share the same native code

Foo<int>, Foo<long> and Foo<float> each have their own copy

How about:

class Foo<T,U>
{
}

I assume that in this case Foo<string,StringBuilder> and
Foo<Stream,StreamWriter> would share the same native code, too. As would
Foo<string,int> and Foo<StringBuilder,int>. Right?
 
R

Richard Blewett [DevelopMentor]

Cool Guy said:
"Richard Blewett [DevelopMentor]" <richard at nospam dotnetconsult dot co
dot said:
[...]

class Foo<T>
{
}

Foo<string> and Foo<SqlConnection> can share the same native code

Foo<int>, Foo<long> and Foo<float> each have their own copy

How about:

class Foo<T,U>
{
}

I assume that in this case Foo<string,StringBuilder> and
Foo<Stream,StreamWriter> would share the same native code, too. As would
Foo<string,int> and Foo<StringBuilder,int>. Right?

Thats my understanding, yes

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
 

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