473,385 Members | 1,901 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Generic Type Instantiation

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?
Nov 17 '05 #1
4 2083
"Cool Guy" <co*****@abc.xyz> wrote in message
news:15****************@cool.guy.abc.xyz...
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
Nov 17 '05 #2
"Richard Blewett [DevelopMentor]" <richard at nospam dotnetconsult dot co
dot uk> wrote:
[...]

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?
Nov 17 '05 #3
"Cool Guy" <co*****@abc.xyz> wrote in message
news:1q**************@cool.guy.abc.xyz...
"Richard Blewett [DevelopMentor]" <richard at nospam dotnetconsult dot co
dot uk> wrote:
[...]

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
Nov 17 '05 #4
"Richard Blewett [DevelopMentor]" <richard at nospam dotnetconsult dot co
dot uk> wrote:
Thats my understanding, yes


Thanks. :)
Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Joanna Carter \(TeamB\) | last post by:
I want to be able to use a generic type, bind its generic parameters to specific type(s) and then hold newly created instances in a list of generic ypes. Sort of like this : class Agent<T> {...
8
by: Cool Guy | last post by:
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...
6
by: Dan Holmes | last post by:
I have a class that i need a constraint of int, string, float or bool. I have tried the following but can't make VS accept it. I read the docs and they showed that any value type can be used...
3
by: Boris | last post by:
I have a class which should like this ideally: generic <typename T> public ref class ManagedClass { T ^managedMember; UnmanagedClass<U*unmanagedMember; }; I actually would like to specify...
7
by: Dave | last post by:
I've got these declarations: public delegate void FormDisplayResultsDelegate<Type>(Type displayResultsValue); public FormDisplayResultsDelegate<stringdisplayMsgDelegate; instantiation:...
3
by: Peter K | last post by:
Hi I have an interface for a "factory" defined like this: public interface IDeviceFactory<T: IDisposable { T GetDevice(); }
11
by: paul.gibson | last post by:
A simple code example is easier than trying to describe the issue. I have: public class myClassA {
11
by: Scott Stark | last post by:
Hello, The code below represents a singly-linked list that accepts any type of object. You can see I'm represting the Data variable a System.Object. How would I update this code to use...
8
by: Advait Mohan Raut | last post by:
Hello, I am using VC# 2005 ; C# 2.0 I am working on the performance measurement tool. For that I need my code to call user defined method along with its parameters. For that should I use a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.